2015-10-15 171 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();