Can anybod tell my why java.util.Scanner is throwing this exeption? - java.util.scanner

import java.util.Scanner;
public class FractionTester
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int num3 = scan.nextInt();
int num4 = scan.nextInt();
}
}
This code keeps throwing the error
FractionTester.java: Line 10: java.util.NoSuchElementException
can anybody explain why or how to fix it, please? Thank you all in advance.

are you sure you are passing 4 input values to the prompt because as its name says NoSuchElementException means scan.nextInt() is not getting any element.
I had tried you code in local and its working for me while passing 4 input values.
see the below code I am giving 4 values to it
now in here see I am giving only 3 values so it throw the exception

Related

Error when calling a function in C# because variable is used in parameter [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm new to c#, but I'm trying to create a basic program that can use a function that adds to numbers (this is just practice I know it's inefficient.
{
int AddNumbers(int num1, int num2) //Here's where the error comes
{
int result = num1 + num2;
return result;
}
int num1 = 10;
int num2 = 20;
AddNumbers(num1, num2);
However, when I try it, it says that "A local parameter named 'num1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter". I assume that this is because I declared the variables while calling the function, but I don't know how to fix it.
Thanks for any help!
EDIT: Just to be clear, the numbers after the functions are the number I would like to be added in the function
Local methods have access to every thing in the parent block. That is why you cannot define the same variable name more than once. Try this instead.
{
int num1;
int num2;
int AddNumbers(int left, int right) => left + right;
AddNumbers(num1, num2);
}
There is a guide for this as well found here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions
I guess you were trying to make a method for add Numbers. If you want to make a method, you need make method OUTSIDE of the Main code.
public static int AddNumbers(int num1, int num2)
{
int sum;
sum = num1 + num2;
return sum;
}
static void Main(string[] args)
{
int a = 10;
int b = 20;
Console.WriteLine($"a = {a}, b = {b}");
//First way to control the string: put $ infront of the string, and write code in the {})
Console.WriteLine("a + b = {0}", AddNumbers(a, b));
//Second way to control the string: put {} and number (start with 0) and write code after comma
}
If you write as this, you will get an answer as
a = 10, b = 20
a + b = 30
For public static int AddNumbers(int num1, int num2)
You don't have to think about public static yet.
At the position of int, you can put another variables such as string, long, double. For that case, you have to return (various you put); at the end.
If you put void, you don't have to put return and the thing you wanted will be done in the method.
Welcome to SO.
as you know you cannot have same variable name used in a method
here is what you need
{
int AddNumbers(int num1, int num2) //Here's where the error comes
{
int result = num1 + num2;
return result;
}
int num2 = 10;
int num3 = 20;
AddNumbers(num2, num3);
}
you can have something like this:
class Program
{
int p = 0;
public static void Main(string[] args)
{
int num1 = 10;
int num2 = 20;
int num = Method1(num1,num2);
Console.WriteLine(num);
}
public static int Method1(int num1, int num2)
{
p = num1 + num2;
return p;
}
}

groovy.lang.MissingMethodException String vs java.lang.String

I was doing a coding challenge that prints a given text in a zig-zag:
thisisazigzag:
t a g
h s z a
i i i z
s g
So I have my code (not sure if it's right or not yet, but that's not part of the question)
class Main {
public void zigzag(String text, int lines) {
String zigLines = [];
while(lines > 0){
String line = "";
increment = lines+(lines-2);
lines = lines + (" " * (lines-1));
for(int i=(lines-1); i<text.length(); i+=increment) {
line = line + text[i] + (" " * increment);
}
zigLines.add(0, line);
lines--;
}
for(line in zigLines){
println(line);
}
}
static void main(String[] args) {
zigzag("thisisazigzag", 4);
}
}
But when I run the script, I keep getting this error:
groovy.lang.MissingMethodException: No signature of method: static Main.zigzag()
is applicable for argument types: (String, Integer) values: [thisisazigzag, 4]
Possible solutions: zigzag(java.lang.String, int)
And I'm very confused as to the difference between java.lang.String and String and then Integer and int?
Any help or explanation of this would be great!
Thanks.
You should make your zigzag method static.
Your code wasn't working because without the static modifier zigzag was an instance method, meaning you would need an instance of your class Main to be able to call it. Here's an introductory tutorial explaining some of these concepts: docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Converting Strings in C#

I want to convert my string value to int16 and only show 2 decimal places. I have tried the below, but it throws an error as my string value is not actually converted?
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string number1 = "1234.00011";
Console.Write(number1);
Console.WriteLine();
string r = String.Format("{0:F2}", number1);
Console.Write(Convert.ToInt16(r));
}
}
}
EDIT
The line that throws the error is
Console.Write(Convert.ToInt16(r));
And the error is
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
Your call to string.Format is using a floating point format specifier and providing a string argument. These are mutually incompatible. As a result, the string argument is inserted as-is into the result, and stored in r, so that you don't get the desired two-digit rounding you're looking for.
What you likely want to do is something like this:
static void Main(string[] args)
{
string number1 = "1234.00011";
Console.Write(number1);
Console.WriteLine();
var r1 = float.Parse(number1);
string r = String.Format("{0:F2}", r1);
Console.WriteLine(r);
Console.Write((int)r1);
Console.ReadKey();
}
In my tests, it produces the following output:
1234.00011
1234.00
1234
Note that the second line has the desired rounding, because we provided a floating point value to string.Format, so that {0:F2} would work properly. The third line has no decimal places, because we used a direct cast and includes no floating point digits whatsoever.

Exception in thread "main" java.util.NoSuchElementException Runtime error getting proper output

Given a number N, find the sum of all products x*y such that N/x = y (Integer Division).
Since, the sum can be very large, please output this modulo 1000000007.
Input
The first line of input file contains an integer T, the number of test cases to follow. Each of the next T lines contain an integer N.
Output
Output T lines containing answer to corresponding test case.
`
private static Scanner sc;
public static void main(String [] args ){
sc = new Scanner(System.in);long k;int s;
int t = sc.nextInt();
while(t>0){
k=0;
long n = sc.nextInt();
for(int i=1;i<=n;i++){
k=k+(n/i)*i;
}
s=(int) (k%1000000007);
System.out.println(s);
}t--;
}
I didn't get any such exception while executing the above program. But you should put t-- inside the while loop. Otherwise it will run infinitely.

How can i make the value the the user inputs appear next to thew print statement?

import java.util.Scanner;
public class nameP{
public static void main(String[] args){
Scanner user = new Scanner(System.in);
int value;
System.out.println("Type a number: ");
value = user.nextInt();
if (value % 2 == 0)
System.out.println("even");
else
System.out.println("odd");
}
}
Need help making the value given by the user appear right next to the print statement....and not in the bottom as it commonly happens. Help would be greatly appreciated.
Use System.out.print
instead of System.out.println
println prints a newline after the String you send it.
System.out.println("The number " + value + " is even");

Resources