Scanner NoSuchElementException

烈酒焚心 提交于 2021-02-05 11:13:15

问题


I made a program that asks for 3 integers to output type of triangle. Everything runs and compiled successfully, however, it seems the part where it asks the user to see if they want to loop it again, the online compiler outputs the error:

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1347) at Assignment5.main(Assignment5.java:56)

import java.util.Scanner;
    public class Assignment5 {

    public static void main (String[]args)
    {


        for (int a = 0; a < Integer.MAX_VALUE; a++)
        {
        Scanner userInput = new Scanner(System.in);
        Scanner answer = new Scanner(System.in);

        int x,y,z;

        System.out.println("Enter the sides of the triangle: ");


        x = userInput.nextInt();
        y = userInput.nextInt();
        z = userInput.nextInt();
        Tri isos = new Tri(x,y,z);
        Tri equal = new Tri(x,y,z);
        Tri scalene = new Tri(x,y,z);



          // check the equilateral triangle
          System.out.println(equal.toString() + " triangle:");


          if (equal.is_isosceles())
             System.out.println("\tIt is isosceles");
          else
             System.out.println("\tIt is not isosceles");

          if (equal.is_equilateral())
             System.out.println("\tIt is equilateral");
          else 
             System.out.println("\tIt is not a equilateral");

          if (equal.is_scalene())
             System.out.println("\tIt is scalene");
          else
             System.out.println("\tIt is not scalene");

          System.out.println("Would you like to enter values again? (y/n)" );

          String input = answer.next();   //Exception is thrown from here

          if (input.equals("y"))
          {
              System.out.println("ok");
          }
              else if(!input.equals("y"))
              {
                  System.out.println("Ok, bye.");
                  break;
              }

        }
    }
    }

回答1:


NoSuchElementException:

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

You're getting this exception because Scanner#next doesn't read the new line character, which is the character when you press enter (\n), so in the next for iteration, you're trying to read it, which causes the exception.

One possible solution is to add answer.nextLine() right after answer.next() in order to swallow this extra \n.


Example of your code:

Iteration (a) |  input for scanner    |  Data for scanner
--------------+-----------------------+-------------------
      0       |   "Hello" (And enter) |       Hello
      1       |         \n            |      PROBLEM!



回答2:


to me it seems that answer.next() does not actually have any value assigned to it usually int name = answer.next() name is assigned what ever answer is. What i mean is that name cant be assigned a value because answer.next() doesn't have one.

At least this is my understanding. The alternative is the get rid of answer.next and use the other scanner. actually an edit to this.

a scanner reads from files or the console. You have one scanner already (userInput) the second scanner isn't actually doing anything as well as it being an actual scanner, it doesn't have anything to read. get rid of answer as a scanner, replace is with an int, String, double and have int answer = userInput.nextInt(); or double answer = userInput.nextDouble(); or String answer = userInput.nextLine();




回答3:


As you said the code runs for you but doesn't when compiled and executed on an online compiler. The answer scanner is exhausted because it doesn't have any elements.

It's embarrassing but i once got the same error when compiling my code on an online compiler, it turned out i wasn't supplying input beforehand to the input section and was expecting the online compiler to ask for the input.

Since you are using two scanners to get input from console, try using the scanner userInput to take the input from a file instead. (It may vary for different online compilers, but there will be an option to provide input from file)



来源:https://stackoverflow.com/questions/19267772/scanner-nosuchelementexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!