2015-10-15 135 views
0

我编写了这个旨在读取整数值文件的代码。如果整数值> = 0和< = 100我需要给出等级的平均值。如果超出指定范围0-100的任何值,则需要计算不正确的整数等级,通知用户不正确的等级,并通知有多少不正确的等级。我试图代码,但我不断收到错误代码:Java循环混淆

Exception in thread "main" java.util.NoSuchElementException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at Project9.main(Project9.java:26) 

代码示例:

public static void main(String[] args) throws IOException{ 
    String file; 
    int readInts; 

    Scanner k = new Scanner(System.in); 

    System.out.println("Enter filename: "); 
    file = k.nextLine(); 
    int counterWrong = 0; 
    int counterRight = 0; 
    int sum = 0; 
    double average = 1.0 * sum/counterRight; 

    File fileReader = new File(file); 

    if (fileReader.exists()) { 
     Scanner input = new Scanner(fileReader); 
     while (input.hasNext()) { 
      readInts = input.nextInt(); 
      System.out.println(readInts); 
      String a = input.next(); 
      int a2 = Integer.parseInt(a); 

     if (a2 <= 100 && a2 >= 0){ 
      counterRight++;    
      sum = sum + a2; 
      System.out.println("Score " + a2 + " was counted."); 

     } else { 
      counterWrong++; 
      System.out.println("The test grade " + a2 + " was not scored as it was out of the range of valid scores."); 
      System.out.println("There were " + counterWrong + " invalid scores that were not counted."); 
     } 
     } 
     if (counterRight > 0){ 
      System.out.println("The average of the correct grades on file is " + average + "."); 
     } 
    } else { 
     System.out.println("The file " + file + " does not exist. The program will now close."); 
    } 


} 

}

+1

请告诉我们您正在阅读 –

+0

您正在阅读2令牌的文件,而循环,但你只检查'hasNext()'在一个只有最prbably你没有留在文件中的任何标记,你仍然用'input.next()'读取文件在这行之前也检查一下 – silentprogrammer

回答

0

有可能是您的代码,我看到两个问题。

  1. file = k.nextLine(); //根据你的文件的设置方式,k.nextLine()或者k.next()或者k.nextInt()可能是有用的。 (input.hasNext()){ readInts = input.nextInt();}} // input.hasNext()假定扫描器正在读取的下一个值有一个字符串值,它将使readInts = input.nextInt();不解析(或其他方法)不可能使用。

我觉得尝试这个练习会很有趣(不想毁了你)。看看我的代码,希望你能拿起我正在谈论的一些概念。

注意:我的程序读取的整数值如95 185 23 13 90 93 37 125 172 99 54 148 53 36 181 127 85 122 195 45 79 14 19 88 34 73 92 97 200 167 126 48 109 38.哪种用法hasNext ()& next()获取列出的每个令牌。所以使用nextLine()对于给定的输入没有用处。

package cs1410;

import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 

import javax.swing.JFileChooser; 

public class Grader { 

    public static void main(String[] args) throws IOException { 
     int count = 0; 
     int sum = 0; 
     double ave = 0; 
     int incorrectCount = 0; 
     String correctGrades = ""; 
     String incorrectGrades = ""; 

     // Read file input 
     JFileChooser chooser = new JFileChooser(); 
     if (JFileChooser.APPROVE_OPTION != chooser.showOpenDialog(null)) { 
      return; 
     } 
     File file = chooser.getSelectedFile(); 

     // Scan chosen document 
     Scanner s = new Scanner(file); 


     // While the document has an Int 
     while (s.hasNextInt()) { 
      // Convert our inputs into an int 
      int grade = Integer.parseInt(s.next()); 

      if (grade >= 0 && grade <= 100) { 
       // adds sum 
       sum += grade; 
       // increments correct count 
       count++; 
       // displays valid grades 
       correctGrades += Integer.toString(grade) + "\n"; 
      } else { 
       // increments incorrect count 
       incorrectCount++; 
       // displays invalid grades 
       incorrectGrades += Integer.toString(grade) + "\n"; 
      } 
     } 
     // Created average variable 
     ave = sum/count; 

     // bada bing bada boom 
     System.out.println("The number of correct grades were " + correctGrades); 
     System.out.println("The average score on this test was " + ave + "\n"); 
     System.out.println("The number of incorrect grades were " + incorrectCount + "\n"); 
     System.out.println("The incorrect values for the grades were " + "\n" + incorrectGrades); 

    } 

} 
+0

完美,谢谢。我的程序还有另一个步骤,我将以新线程的形式发布。我最后的问题更简单。 –

1

你正在做一个检查hasNext但你使用nextInt()扫描仪读取两次和next()

0

使用hasNextInt()而不是hasNext()。

hasNext()仅表示存在另一个标记,并不一定表示您在编写nextInt()时会假设有另一个整数。

下面是hasNext()和文档hasNextInt()

您还希望做这行之前的检查:

String a = input.next();