修复扫描仪的java.util.NoSuchElementException

修复扫描仪的java.util.NoSuchElementException,java,java.util.scanner,Java,Java.util.scanner,如何解决java.util.NoSuchElementException错误?我调用scanner对象3次,程序将接受前两次输入。但是在第三次尝试中,我立即得到了java.util.NoSuchElementException错误。扫描仪对象的名称为stdInput 我已经尝试为抛出此错误的实例创建一个新的scanner对象,但仍然得到相同的错误,只是来自代码中的另一行 /** * reddit url: https://www.reddit.com/r/dailyprogrammer/co

如何解决
java.util.NoSuchElementException
错误?我调用scanner对象3次,程序将接受前两次输入。但是在第三次尝试中,我立即得到了
java.util.NoSuchElementException
错误。扫描仪对象的名称为
stdInput

我已经尝试为抛出此错误的实例创建一个新的scanner对象,但仍然得到相同的错误,只是来自代码中的另一行

/**
 * 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
/**
*reddit url:https://www.reddit.com/r/dailyprogrammer/comments/pjbj8/easy_challenge_2/
*/
导入java.util.Scanner;
公开课挑战赛{
公共静态void main(字符串[]args){
int-choiceNum=0;
布尔continueRunningProgram=true;
字符串repeatProgram=“”;
扫描仪标准输入=新扫描仪(System.in);
做{
系统输出println(“你是在解力(1)、质量(2)还是加速度(3)”;
choiceNum=stdInput.nextInt();
如果(isValidChoiceNum(choiceNum)=false){
做{
System.out.println(“数字“+choiceNum+”是无效的选择。请重新选择。”);
choiceNum=stdInput.nextInt();
}while(isValidChoiceNum(choiceNum)=false);
}
交换机(选配){
案例1:
System.out.println(“力是”+solvingforforforce());
打破
案例2:
System.out.println(“质量为”+solvingForMass());
打破
案例3:
System.out.println(“加速度为”+solvingForAcceleration());
打破
}
System.out.println(“您想解决另一个涉及力、质量和加速度(Y/N)的问题吗?”;
repeatProgram=stdInput.next();
if(isValidChoiceChar(repeatProgram)=false){
做{
System.out.println(“字母“+repeatProgram+”是无效的选择。请重新选择。”);
repeatProgram=stdInput.next();
}while(isValidChoiceChar(repeatProgram)=false);
}
if(repeatProgram.compareTo(“Y”)==0){
continueRunningProgram=true;
}否则{
continueRunningProgram=false;
}
}while(continueRunningProgram==true);
stdInput.close();
}//主方法结束
公共静态布尔值isValidChoiceNum(int c){
如果(c<1 | | c>3){
返回false;
}否则{
返回true;
}
}
公共静态布尔值isValidChoiceChar(字符串c){
如果(c.compareTo(“Y”)==0 | | c.compareTo(“N”)==0){
返回true;
}否则{
返回false;
}
}
公共静态双解force(){
扫描仪stdInput2=新扫描仪(System.in);
System.out.println(“请输入质量值”);
double m=stdInput2.nextDouble();
System.out.println(“请输入加速度值”);
双a=stdInput2.nextDouble();
stdInput2.close();
返回m*a;
}
公共静态双解形式(){
扫描仪stdInput2=新扫描仪(System.in);
System.out.println(“请输入force的值”);
double f=stdInput2.nextDouble();
System.out.println(“请输入加速度值”);
双a=stdInput2.nextDouble();
stdInput2.close();
返回f/a;
}
公共静态双解加速(){
扫描仪stdInput2=新扫描仪(System.in);
System.out.println(“请输入force的值”);
double f=stdInput2.nextDouble();
System.out.println(“请输入质量值”);
double m=stdInput2.nextDouble();
stdInput2.close();
返回f*m;
}
}//下课

停止疯狂关闭链接到
系统的
扫描仪!它将关闭底层流(
System.in
),导致从该
读取的任何其他尝试引发异常

是的,您将得到一个警告,但忽略该警告是安全的,或者您可以添加

@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
为了避免它

记住,如果没有打开资源,就不应该关闭它。让JVM在终止程序时关闭它


这还包括在try with resources块内创建
扫描仪(System.in)

try(Scanner in = new Scanner(System.in)){
   //Code... 
} 
这将隐式关闭块末尾的流

从:

try with resources语句确保在语句末尾关闭每个资源


另外,在
中,有几个
扫描仪
对象从
系统中读取数据,这是一种不好的做法。我会将它们作为参数传递给您的方法:

public static double solvingForMass(Scanner in) {

    System.out.println("Please enter a value for force.");
    double f = in.nextDouble();

    System.out.println("Please enter a value for acceleration.");
    double a = in.nextDouble();

    return f / a;

}

另外,请注意,如果您正在进行以下结构:

if(someBool)
   return true;
else
   return false;
它可以简化为

return someBool
因此,您的
isValidChoice()
方法可以简化为:

public static boolean isValidChoiceChar(String c) {
     return c.compareTo("Y") == 0 || c.compareTo("N") == 0;
}

尽管注意,您可以使用
equals()
和equalsIgnoreCase()
方法来比较
字符串`

,但不要关闭
扫描仪
系统中读取。
!非常感谢@GBlodgett!!!你不仅解决了我的问题,还给了我其他一些有用的建议。请随时提供您可能有的任何其他建议。我真的很感激。@neosour我唯一的建议是在你的
if
语句中,
if(validChoice(choice)==false)
可以重写为
if(!validChoice(choice))
明白了。再次感谢您的建议!