2013-10-09 57 views
0

我做了一个程序,要求3个整数输出类型的三角形。一切运行和编译成功,但是,它似乎在它要求用户在看他们是否想再次循环它的一部分,在线编译器输出错误:扫描仪NoSuchElementException

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; 
       } 

     } 
    } 
    } 
+0

什么是56行?我也想知道为什么你需要2台扫描仪。 –

+0

line 56: String input = answer.next(); \t \t 如果(input.equals( “Y”)) \t { \t \t System.out的。的println( “OK”); \t} \t否则,如果 \t { \t \t的System.out.println( “好吧,再见。”)(input.equals( “Y”)!); \t \t break; \t} – Ryan

+0

经过测试,它适用于我。 – Aneesh

回答

1

NoSuchElementException

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

你得到这个例外,因为Scanner#next不是阅读新行字符,这是当你按下输入(\n)字符,所以在下一个for迭代,您正试图读取它,这会导致异常。

一个可能的解决方案是为了answer.next()之后添加answer.nextLine()燕子这个额外\n。你的代码的


例子:

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

我试图将其纳入其中,但我不明白。 – Ryan

+0

试过这个,仍然得到同样的错误。 – Ryan

+0

你把它放在哪里? – Maroun

0

我看来answer.next()并不真正分配给它 通常诠释名称的任何值= answer.next()的名字是分配了什么答案是。我的意思是名称不能被赋值,因为answer.next()没有。

至少这是我的理解。另一种方法是摆脱answer.next并使用其他扫描仪。 实际上是对此进行编辑。

扫描仪从文件或控制台读取。你已经有一台扫描仪(用户输入),第二台扫描仪实际上并没有做任何事情,它是一台真正的扫描仪,它没有任何可读的内容。 摆脱作为扫描仪的答案,替换是用int,String,double和 int answer = userInput.nextInt(); 或 double answer = userInput.nextDouble(); 或 String answer = userInput.nextLine();

0

正如你所说的代码为你运行,但没有编译和执行在线编译器。答案扫描器已经耗尽,因为它没有任何元素。

这很令人尴尬,但是我在编译在线编译器时编译我的代码时遇到了同样的错误,事实证明,我并未事先向输入部分提供输入,并期望在线编译器要求输入。

由于您正在使用两台扫描仪从控制台获取输入,请尝试使用扫描仪userInput从文件中接受输入。 (它可能会因不同的在线编译器而有所不同,但可以选择从文件中提供输入)