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
Filtering Database Rows with Spring-Data-JPA and Spring-Mvc
Convert a String of Hex into Ascii in Java
Comparing Arrays in Junit Assertions, Concise Built-In Way
Change Location of Log4J.Properties
Why Are Getter and Setter Method Important in Java
How to Correctly Use Custom Renderers to Paint Specific Cells in a Jtable
Missing Return Statement in a Non-Void Method Compiles
What Is the Purpose of Defining a Package in a Java File
Spring Qualifier and Property Placeholder
Including Images with an Executable Jar
Reserved Words as Names or Identifiers
Java, Shifting Elements in an Array
Jersey Maven Quickstart Archetype in Eclipse
How to Set Classpath When I Use Javax.Tools.Javacompiler Compile the Source