How do I resolve the java.util.NoSuchElementException
error? I call the scanner object 3 times, and the program will accept input the first two times. But on the third try, I immediately get the java.util.NoSuchElementException
error. The name of the scanner object is stdInput
.
I've tried creating a new scanner object just for the instance throwing this error, but I still get the same error, just from a different line in the code.
/**
* reddit url: https://www.reddit.com/r/dailyprogrammer/comments/pjbj8/easy_challenge_2/
*/
import java.util.Scanner;
public class challenge {
public static void main(String[] args) {
int choiceNum = 0;
boolean continueRunningProgram = true;
String repeatProgram = "";
Scanner stdInput = new Scanner (System.in);
do {
System.out.println("Are you solving for force (1), mass (2), or acceleration (3)?");
choiceNum = stdInput.nextInt();
if(isValidChoiceNum(choiceNum) == false) {
do {
System.out.println("The number " + choiceNum + " is an invalid choice. Please choose again.");
choiceNum = stdInput.nextInt();
} while(isValidChoiceNum(choiceNum) == false);
}
switch(choiceNum) {
case 1:
System.out.println("The force is " + solvingForForce());
break;
case 2:
System.out.println("The mass is " + solvingForMass());
break;
case 3:
System.out.println("The acceleration is " + solvingForAcceleration());
break;
}
System.out.println("Would you like to solve another problem involving force, mass, and acceleration (Y/N)?");
repeatProgram = stdInput.next();
if(isValidChoiceChar(repeatProgram) == false) {
do {
System.out.println("The letter " + repeatProgram + " is an invalid choice. Please choose again.");
repeatProgram = stdInput.next();
} while(isValidChoiceChar(repeatProgram) == false);
}
if(repeatProgram.compareTo("Y") == 0) {
continueRunningProgram = true;
} else {
continueRunningProgram = false;
}
} while(continueRunningProgram == true);
stdInput.close();
} // end of main method
public static boolean isValidChoiceNum(int c) {
if(c < 1 || c > 3 ) {
return false;
} else {
return true;
}
}
public static boolean isValidChoiceChar(String c) {
if(c.compareTo("Y") == 0 || c.compareTo("N") == 0) {
return true;
} else {
return false;
}
}
public static double solvingForForce() {
Scanner stdInput2 = new Scanner (System.in);
System.out.println("Please enter a value for mass.");
double m = stdInput2.nextDouble();
System.out.println("Please enter a value for acceleration.");
double a = stdInput2.nextDouble();
stdInput2.close();
return m * a;
}
public static double solvingForMass() {
Scanner stdInput2 = new Scanner (System.in);
System.out.println("Please enter a value for force.");
double f = stdInput2.nextDouble();
System.out.println("Please enter a value for acceleration.");
double a = stdInput2.nextDouble();
stdInput2.close();
return f / a;
}
public static double solvingForAcceleration() {
Scanner stdInput2 = new Scanner (System.in);
System.out.println("Please enter a value for force.");
double f = stdInput2.nextDouble();
System.out.println("Please enter a value for mass.");
double m = stdInput2.nextDouble();
stdInput2.close();
return f * m;
}
} // end of class