Exception in Thread "Main" Java.Util.Nosuchelementexception

Exception in thread main java.util.NoSuchElementException

You close the second Scanner which closes the underlying InputStream, therefore the first Scanner can no longer read from the same InputStream and a NoSuchElementException results.

The solution: For console apps, use a single Scanner to read from System.in.

Aside: As stated already, be aware that Scanner#nextInt does not consume newline characters. Ensure that these are consumed before attempting to call nextLine again by using Scanner#newLine().

See: Do not create multiple buffered wrappers on a single InputStream

Getting error Exception in thread main java.util.NoSuchElementException

You would get java.util.NoSuchElementException when you try to call nextInt when there is no such element that you have inputted. You could avoid the issue by making use of check with a method call on scanner instance method hasNextInt. I think the problem is with your division operation, where you should check for divisor being 0 instead of dividend and only take divisors new value instead of both.

Couple of things about your code:

a. You are creating transient object within for loop. You could create them outside the loop.

b. You shouldn't need to close scanner if you use try with resource construct which is available since JDK 7.

c. If you ever want to close scanner, you should close it in finally block where it may happen you get an exception and you may end up leaking the resource.

d. Your first if condition for checking option == 5 should be after your while loop while(option>=6 || option<=0){

To sum up, this could be the code to solve your problem:

public static void main(String[] args) {
add addobj= new add();//Use camel case naming convention for classes
subtract subobj= new subtract();
multiplication mulobj= new multiplication();
division divobj= new division();
try(Scanner input = new Scanner(System.in)) {
for(int i=0; i<=4;i++) {
System.out.println("1.ADDITION\n2.SUBTRACTION\n3.MULTIPLICATION\n4.DIVISION\n5.EXIT");
System.out.print("Choose Your Operator:");
int option= input.nextInt();

while(option>=6 || option<=0){
System.out.println("Please select one(1) to five(5).");
option= input.nextInt();
}

if(option==5)
return;

System.out.println("Enter two number:");
int number1= input.nextInt();
int number2=input.nextInt();

switch(option) {
case 1:
System.out.println("Addition:"+"("+ number1 +") + ("+ number2 +") = "+ addobj.result(number1,number2));
break;
case 2:
System.out.println("Subtraction:"+"("+ number1 +") - ("+ number2 +") = "+ subobj.result(number1,number2));
break;
case 3:
System.out.println("Multiplication:"+"("+ number1 +") * ("+ number2 +") = "+ mulobj.result(number1,number2));
break;
case 4:
while(number2==0){
System.out.println("Please enter non-zero value of second number:");
number2= input.nextInt();
}
System.out.println("Division:"+ "("+ number1 +") / ("+ number2 +") = "+ divobj.result(number1,number2));
break;
}
System.out.println("\n");
}
}
}

`Enter Activity Number: Exception in thread main java.util.NoSuchElementException

In the class Act1 you call

 input.close();

it closes your System.in stream in the end of the 1st iteration.

In the class mid_term you have an infinite loop, if you want your program work without NoSuchElementException, you should keep your stream open. Delete the

input.close();

How to fix runtime error - Exception in thread main java.util.NoSuchElementException

Try this

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i=0; i<=T; i++){
if(!sc.hasNext()) break;
String S = sc.next();

for(int j=0; j<S.length(); j++){
if(j%2==0){
System.out.print(S.charAt(j));
}
}

System.out.print(" ");

for(int r=0; r<S.length(); r++){
if(r%2!=0){
System.out.print(S.charAt(r));
}
}
System.out.println("");

}
}

java.util.NoSuchElementException is thrown when there is no next element. To avoid this you should check by using hasNext().

Read this for more details : https://www.tutorialspoint.com/java/util/scanner_hasnext.htm



Related Topics



Leave a reply



Submit