2015-07-01 254 views
0

我的错误信息是这样的:为什么我会收到错误“java.util.NoSuchElementException”?

Exception in thread "main" java.util.NoSuchElementException: No line found 
    at java.util.Scanner.nextLine(Scanner.java:1540) 
    at GPA3.answer(GPA3.java:364) 
    at GPA3.main(GPA3.java:355) 

我有我的主要方法和我的代码工作完美的休息,但一旦我添加的代码,这一块它抛出一个错误。它不允许我输入任何东西到扫描仪中。

import java.util.Scanner; 
public class GPA3 { 

    public static void main(String[] args) 
    { 
     System.out.println("Hello! Welcome to the GPA Calculator."); 

     int Math; 
     double mathGpa = 0; 

     System.out.println("Math = "); 
     Scanner input = new Scanner(System.in); 
     Math= input.nextInt(); 

     if (Math >100){ 
      System.out.println("You have mistyped something"); 
     } 
     if (Math >= 94){ 
      System.out.println("You have an A"); 
      mathGpa = 4.0; 
      System.out.println(mathGpa); 
     } 
     if (Math < 94 && Math >=90){ 
      System.out.println("You have an A-"); 
      mathGpa = 3.7; 
      System.out.println(mathGpa); 
     } 
     if (Math < 90 && Math >=87){ 
      System.out.println("You have a B+"); 
      mathGpa = 3.3; 
      System.out.println(mathGpa); 
     } 
     if (Math < 87 && Math >=80){ 
      System.out.println("You have a B"); 
      mathGpa = 3.0; 
      System.out.println(mathGpa); 
     } 
     if (Math < 80 && Math >=77){ 
      System.out.println("You have a B-"); 
      mathGpa = 2.7; 
      System.out.println(mathGpa); 
     } 
     if (Math < 77 && Math >=73){ 
      System.out.println("You have a C+"); 
      mathGpa = 2.3; 
      System.out.println(mathGpa); 
     } 
     if (Math < 73 && Math >=70){ 
      System.out.println("You have a C"); 
      mathGpa = 2.0; 
      System.out.println(mathGpa); 
     } 
     if (Math < 70 && Math >=67){ 
      System.out.println("You have a C-"); 
      mathGpa = 1.7; 
      System.out.println(mathGpa); 
     } 
     if (Math < 67 && Math >=67){ 
      System.out.println("You have a D+"); 
      mathGpa = 1.3; 
      System.out.println(mathGpa); 
     } 
     if (Math < 67 && Math >=63){ 
      System.out.println("You have a D"); 
      mathGpa = 1.0; 
      System.out.println(mathGpa); 
     } 
     if (Math < 63 && Math >=60){ 
      System.out.println("You have a D-"); 
      mathGpa = 0.7; 
      System.out.println(mathGpa); 
     } 
     if (Math < 60){ 
      System.out.println("You have a F"); 
      mathGpa = 0.0; 
      System.out.println(mathGpa); 
     }//End of Math 

     int English; 
     double englishGpa = 0; 
     System.out.println("English = "); 
     English= input.nextInt(); 

     if (English >100){ 
      System.out.println("You have mistyped something"); 
     } 
     if (English >= 94 && English <101){ 
      System.out.println("You have an A"); 
      englishGpa = 4.0; 
      System.out.println(englishGpa); 
     } 
     if (English < 94 && English >=90){ 
      System.out.println("You have an A-"); 
      englishGpa = 3.7; 
      System.out.println(englishGpa); 
     } 
     if (English < 90 && English >=87){ 
      System.out.println("You have a B+"); 
      englishGpa = 3.3; 
      System.out.println(englishGpa); 
     } 
     if (English < 87 && English >=80){ 
      System.out.println("You have a B"); 
      englishGpa = 3.0; 
      System.out.println(englishGpa); 
     } 
     if (English < 80 && English >=77){ 
      System.out.println("You have a B-"); 
      englishGpa = 2.7; 
      System.out.println(englishGpa); 
     } 
     if (English < 77 && English >=73){ 
      System.out.println("You have a C+"); 
      englishGpa = 2.3; 
      System.out.println(englishGpa); 
     } 
     if (English < 73 && English >=70){ 
      System.out.println("You have a C"); 
      englishGpa = 2.0; 
      System.out.println(englishGpa); 
     } 
     if (English < 70 && English >=67){ 
      System.out.println("You have a C-"); 
      englishGpa = 1.7; 
      System.out.println(englishGpa); 
     } 
     if (English < 67 && English >=67){ 
      System.out.println("You have a D+"); 
      englishGpa = 1.3; 
      System.out.println(englishGpa); 
     } 
     if (English < 67 && English >=63){ 
      System.out.println("You have a D"); 
      englishGpa = 1.0; 
      System.out.println(englishGpa); 
     } 
     if (English < 63 && English >=60){ 
      System.out.println("You have a D-"); 
      englishGpa = 0.7; 
      System.out.println(englishGpa); 
     } 
     if (English < 60){ 
      System.out.println("You have a F"); 
      englishGpa = 0.0; 
      System.out.println(englishGpa); 
     }//End of English 

     int Science; 
     double SciGpa = 0; 
     System.out.println("Science = "); 
     Science= input.nextInt(); 

     if (Science >100){ 
      System.out.println("You have mistyped something"); 
     } 
     if (Science >= 94 && Science <101){ 
      System.out.println("You have an A"); 
      SciGpa = 4.0; 
      System.out.println(SciGpa); 
     } 
     if (Science < 94 && Science >=90){ 
      System.out.println("You have an A-"); 
      SciGpa = 3.7; 
      System.out.println(SciGpa); 
     } 
     if (Science < 90 && Science >=87){ 
      System.out.println("You have a B+"); 
      SciGpa = 3.3; 
      System.out.println(SciGpa); 
     } 
     if (Science < 87 && Science >=80){ 
      System.out.println("You have a B"); 
      SciGpa = 3.0; 
      System.out.println(SciGpa); 
     } 
     if (Science < 80 && Science >=77){ 
      System.out.println("You have a B-"); 
      SciGpa = 2.7; 
      System.out.println(SciGpa); 
     } 
     if (Science < 77 && Science >=73){ 
      System.out.println("You have a C+"); 
      SciGpa = 2.3; 
      System.out.println(SciGpa); 
     } 
     if (Science < 73 && Science >=70){ 
      System.out.println("You have a C"); 
      SciGpa = 2.0; 
      System.out.println(SciGpa); 
     } 
     if (Science < 70 && Science >=67){ 
      System.out.println("You have a C-"); 
      SciGpa = 1.7; 
      System.out.println(SciGpa); 
     } 
     if (Science < 67 && Science >=67){ 
      System.out.println("You have a D+"); 
      SciGpa = 1.3; 
      System.out.println(SciGpa); 
     } 
     if (Science < 67 && Science >=63){ 
      System.out.println("You have a D"); 
      SciGpa = 1.0; 
      System.out.println(SciGpa); 
     } 
     if (Science < 63 && Science >=60){ 
      System.out.println("You have a D-"); 
      SciGpa = 0.7; 
      System.out.println(SciGpa); 
     } 
     if (Science < 60){ 
      System.out.println("You have a F"); 
      SciGpa = 0.0; 
      System.out.println(SciGpa); 
     }//End of Science 

     int Spanish; 
     double SpanGpa = 0; 
     System.out.println("Spanish = "); 
     Spanish= input.nextInt(); 

     if (Spanish >100){ 
      System.out.println("You have mistyped something"); 
     } 
     if (Spanish >= 94 && Spanish <101){ 
      System.out.println("You have an A"); 
      SpanGpa = 4.0; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 94 && Spanish >=90){ 
      System.out.println("You have an A-"); 
      SpanGpa = 3.7; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 90 && Spanish >=87){ 
      System.out.println("You have a B+"); 
      SpanGpa = 3.3; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 87 && Spanish >=80){ 
      System.out.println("You have a B"); 
      SpanGpa = 3.0; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 80 && Spanish >=77){ 
      System.out.println("You have a B-"); 
      SpanGpa = 2.7; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 77 && Spanish >=73){ 
      System.out.println("You have a C+"); 
      SpanGpa = 2.3; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 73 && Spanish >=70){ 
      System.out.println("You have a C"); 
      SpanGpa = 2.0; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 70 && Spanish >=67){ 
      System.out.println("You have a C-"); 
      SpanGpa = 1.7; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 67 && Spanish >=67){ 
      System.out.println("You have a D+"); 
      SpanGpa = 1.3; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 67 && Spanish >=63){ 
      System.out.println("You have a D"); 
      SpanGpa = 1.0; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 63 && Spanish >=60){ 
      System.out.println("You have a D-"); 
      SpanGpa = 0.7; 
      System.out.println(SpanGpa); 
     } 
     if (Spanish < 60){ 
      System.out.println("You have a F"); 
      SpanGpa = 0.0; 
      System.out.println(SpanGpa); 
     }//End of Spanish 

     int Religion; 
     double RGpa = 0; 
     System.out.println("Religion = "); 
     Religion= input.nextInt(); 
     input.close(); 

     if (Religion >100){ 
      System.out.println("You have mistyped something"); 
     } 
     if (Religion >= 94 && Religion < 101){ 
      System.out.println("You have an A"); 
      RGpa = 4.0; 
      System.out.println(RGpa); 
     } 
     if (Religion < 94 && Religion >=90){ 
      System.out.println("You have an A-"); 
      RGpa = 3.7; 
      System.out.println(RGpa); 
     } 
     if (Religion < 90 && Religion >=87){ 
      System.out.println("You have a B+"); 
      RGpa = 3.3; 
      System.out.println(RGpa); 
     } 
     if (Religion < 87 && Religion >=80){ 
      System.out.println("You have a B"); 
      RGpa = 3.0; 
      System.out.println(RGpa); 
     } 
     if (Religion < 80 && Religion >=77){ 
      System.out.println("You have a B-"); 
      RGpa = 2.7; 
      System.out.println(RGpa); 
     } 
     if (Religion < 77 && Religion >=73){ 
      System.out.println("You have a C+"); 
      RGpa = 2.3; 
      System.out.println(RGpa); 
     } 
     if (Religion < 73 && Religion >=70){ 
      System.out.println("You have a C"); 
      RGpa = 2.0; 
      System.out.println(RGpa); 
     } 
     if (Religion < 70 && Religion >=67){ 
      System.out.println("You have a C-"); 
      RGpa = 1.7; 
      System.out.println(RGpa); 
     } 
     if (Religion < 67 && Religion >=67){ 
      System.out.println("You have a D+"); 
      RGpa = 1.3; 
      System.out.println(RGpa); 
     } 
     if (Religion < 67 && Religion >=63){ 
      System.out.println("You have a D"); 
      RGpa = 1.0; 
      System.out.println(RGpa); 
     } 
     if (Religion < 63 && Religion >=60){ 
      System.out.println("You have a D-"); 
      RGpa = 0.7; 
      System.out.println(RGpa); 
     } 
     if (Religion < 60){ 
      System.out.println("You have a F"); 
      RGpa = 0.0; 
      System.out.println(RGpa); 
     }//End of Religion 
     answer(); 
    }//End of Main 

    public static double answer() 
     {//Begin answer 
     double sClass=0; 
     System.out.println("Do you have gym this semester?"); 
     Scanner input = new Scanner(System.in); 
     String yes = "yes"; 
     String answer = input.nextLine(); 

     if (answer.equals(yes)){ 

     } 
     input.close(); 
     return sClass; 
    }//End answer 

} 
+0

当我删除input.close()时会抛出错误 – user5071017

回答

0

的问题是,你试图打开同一数据流的两个不同Scanner对象 - System.in - 在同一时间(同时开启,这是造成问题,当第二扫描仪将尝试从读。流

你可以做的是让你的answer()函数接受扫描对象作为参数,然后用它代替创建在同流的新的

示例 -

.... // Lots of logic till here 
    }//End of Religion 
    answer(input); 
}//End of Main 

public static double answer(Scanner input) 
    {//Begin answer 
    double sClass=0; 
    System.out.println("Do you have gym this semester?"); 
    String yes = "yes"; 
    String answer = input.nextLine(); 

    if (answer.equals(yes)){ 

    } 
    input.close(); 
    return sClass; 
}//End answer 

您还需要删除input.close();,因为它将关闭基础流,即System.in

+0

感谢您的帮助。它修复了错误,但控制台不允许我输入任何响应。 – user5071017

+0

它做了什么呢? –

+0

您还需要删除'input.close();',因为它会关闭底层的流,它是'System.in'。 更新了相同的答案,请现在检查 –

相关问题