NoSuchElementException错误

时间:2018-07-19 22:42:08

标签: java nosuchelementexception

每次我去编译程序时,都会出现以下错误:

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1478)
    at Pokedex.addPokemon(Pokedex.java:58)
    at PokemonProgram.Menu(PokemonProgram.java:93)
    at PokemonProgram.main(PokemonProgram.java:32)

PokemonProgram类包含我的main方法,这似乎是导致此问题的代码的一部分

(PokemonProgram类)

case 3:                                   
    species = "";
    pokedex.addPokemon(species)
    Menu(scnr);
    break;

(Pokedex类)

public boolean addPokemon(String species) {                         

    boolean success = true;                                          //BOOLEAN FOR SUCCESSFULLY ADDING POKEMON
    dontWannaBeTheVeryBest++;

    if (dontWannaBeTheVeryBest > (Pokemons.length - 1)) {                //TEST FOR EXCEEDING POKEMONS SIZE
        success = false;
        System.out.println("Error: Pokedex is full!");
        dontWannaBeTheVeryBest--;
        System.out.println("");
    }

    if (success) {                                                             //CONTINUES ADDING OBJECT TO ARRAY IF MAX AND DUPLICATE CONDITIONS ARE FALSE
        System.out.print("Enter the name of the species to add: ");
        Scanner scanner = new Scanner(System.in);                            //Calls Pakuri constructor depended upon string
        while (true) {
            if (!scanner.hasNext()) {                                 //SOLUTION TO COMPILER ERROR
                scanner.next();
            } else {
                species = scanner.next();
                break;
            }

            System.out.println("Please enter a valid size.");               //BOOLEAN FOR INVALID INPUT
            System.out.print("Enter max capacity of the Pokedex: ");
        }
        Pokemon store = new Pokemon(species);
        Pokemon[dontWannaBeTheVeryBest] = store;                             //STORES OBJECTS TO ARRAY
        size++;
        System.out.println("Pokemon species " + species + " successfully added!");
        System.out.println("");
    }
        return success;
    //RETURNS WHETHER OR NOT ADDING POKEMON WAS SUCCESSFUL
}

我曾尝试将.next()和.nextLine()弄乱,但这些似乎都不能解决我的问题,我的扫描仪怎么了?

2 个答案:

答案 0 :(得分:0)

如果接受多个输入,则使用while(scanner.hasNext()){species = Scanner.nextLine()}。如果仅查找一个输入(无论它是什么),则只需使用物种= Scanner.nextLine()。如果您只想输入行的第一个单词,请改用scanner.next()

答案 1 :(得分:0)

这是问题代码:

        if (!scanner.hasNext()) {    //SOLUTION TO COMPILER ERROR
            scanner.next();
        } else {
            species = scanner.next();
            break;
        }

保证“ if”语句的“ then”部分抛出java.util.NoSuchElementException。您致电scanner.hasNext(),当它返回false时仍要致电next()

我不知道您在//SOLUTION TO COMPILER ERROR中所指的是什么,但这不是正确的解决方案。

  

我尝试弄乱.next()和.nextLine()...

通常来说,“四处走动”不是设计/实现良好代码的好方法。您需要做的是仔细考虑代码要尝试做的事情。在以下情况下,将以什么顺序询问什么问题,以及代码应该做什么?

  • 可接受的输入值
  • 输入值不可接受
  • 完全没有价值;即EOF

在输入不可接受的情况下(例如,当期望整数时为非整数),您需要移至输入之后。当您只希望输入一个单词时,还需要处理多个“单词”的情况。