java NoSuchElementException:找不到行

时间:2018-10-14 05:53:17

标签: java nosuchelementexception

编辑: 添加以下调用的方法之一,以防万一问题出在方法上而不是循环上。

下面是一个开关菜单,开关中有几种情况。每种情况都从另一个类调用一个public void方法,并成功完成,应该显示菜单方法(打印菜单选项的方法)并允许用户选择另一个选项。目前,完成一个案例后,程序showMenu()再次出现,然后立即中断:

java.util.NoSuchElementException: No line found

在菜单的第二次迭代中并发回

public class Menu {
    public static char selection;
    public static String quitting = "you dun son";
    public static String errorMessage = "THAAAAATSS A menu WRRRAAAAAPPP";

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    try {
        do {
            showMenu();
            String menu = in.nextLine();
            if (menu.length() > 0) {
                selection = menu.toLowerCase().charAt(0); // extract the first char of the line read
            } else {
                selection = '\0';
                System.out.println("invalid input:\t" + selection);
                System.out.println("Press enter to continue...");
            }
            switch (selection) {
                case 'f':
                    FuelConsole fuelObject = new FuelConsole();
                    fuelObject.fuelCalc();
                    break;
                case 'g':
                    GameConsole gameObject = new GameConsole();
                    gameObject.gameCalc();
                    break;
                case 'q':
                    System.out.println("\nEnding Now\n");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Instruction is invalid");
                }
        } while (selection != 'Q' && selection != 'q');
        {
            System.out.println(quitting);
            System.exit(0);
        }
    }catch (Exception e) {
        System.out.println(errorMessage);
    }
}

问题:我需要程序在showMenu()之后暂停或进行类似操作;要让用户输入菜单选择,当前会显示菜单,程序会立即崩溃

欢呼

public class FuelConsole {

    public static String errorMessage = "THAAAAATSS A fuel calc WRRRRAAAAAPPPPPPPPPPPP";
    public static Double acceptableCentsPerLitre = 16.00;
    Scanner scan = new Scanner(System.in);

    public void fuelCalc() {

    try {
        System.out.println("\nyou selected option 'f' --- here you will enter some information and find average fuel for ONE trip. ");
        System.out.println("please enter the amount of fuel in litres:\n ");
        float fuel = scan.nextFloat();

        System.out.println("please enter the price of fuel in cents (not dollars and cents, only CENTS (lulz)):\n ");
        int cent = scan.nextInt();

        System.out.println("please enter the number of kilometers travelled on the tank:\n ");
        float kilo = scan.nextFloat();

        float returnAFC = afc(fuel, kilo);
        float returnAC = ac(returnAFC, cent);

        System.out.println("average consumption: \t" + returnAFC);
        System.out.println("average cost: \t\t" + returnAC);
        if (returnAC > acceptableCentsPerLitre) {
            System.out.println("Average fuel is above 16c per litre");
        } else {
            System.out.println("Average fuel is below 16c per litre");
        }

    } catch (Exception e) {
        System.out.println(errorMessage);
        e.printStackTrace();

    } finally {
        scan.close();
    }

}

public static float afc(float x, float z) {
    float result = x / z;
    return result;
}

public static float ac(float x, int y) {
    float result = x * y;
    return result;
}

一种测试方法:

ng build --prod --aot

}

1 个答案:

答案 0 :(得分:0)

我认为问题在于您在FuelConsole类中创建了一个新的扫描仪。 您还可以在finally块中将其关闭。

但是,当您在扫描仪上调用close()时,它也会同时关闭底层InputStream。 (在这种情况下,{System.in)由于您的顶级Scanner使用相同的InputStream,因此它无法捕获来自它的更多输入。

解决方案可能是将顶级扫描仪作为参数传递给其他对象:

            case 'f':
                FuelConsole fuelObject = new FuelConsole();
                fuelObject.fuelCalc(in);
                break;
            case 'g':
                GameConsole gameObject = new GameConsole();
                gameObject.gameCalc(in);
                break;

public void fuelCalc(Scanner scan) {

try {

...

并删除finally