2016-10-23 64 views
3

編輯:我修復了原來的異常錯誤,感謝蒂姆Biegeleisen通過添加如果(scan.hasNextInt())在我在下面發表評論的行。但輸出文件給我零而不是預期的結果。Java:在輸出文件中得到零

我知道我的代碼非常業餘,但請耐心等待,因爲我還只是一個初學者。非常感謝您提前!

EDIT2: 的文本文件,它從結構像這樣輸入:

50 50 50 50 50 50 
65 73 45 98 90 76 
33 90 75 34 42 55 
56 86 88 99 23 97 
65 78 79 98 70 87 
50 50 50 50 50 50 
65 73 45 98 90 76 
33 90 75 34 42 55 
56 86 88 99 23 97 
65 78 79 98 70 87 
50 50 50 50 50 50 
65 73 45 98 90 76 
33 90 75 34 42 55 
56 86 88 99 23 97 
65 78 79 98 70 87 
50 50 50 50 50 50 
65 73 45 98 90 76 
33 90 75 34 42 55 
56 86 88 99 23 97 
65 78 79 98 70 87 
50 50 50 50 50 50 
65 73 45 98 90 76 
33 90 75 34 42 55 
56 86 88 99 23 97 
65 78 79 98 70 87 
50 50 50 50 50 50 
65 73 45 98 90 76 
33 90 75 34 42 55 
56 86 88 99 23 97 
65 78 79 98 70 87 

EDIT3:這似乎是問題是,它並沒有超越如果一些首位原因。

+0

我添加如果(scan.hasNextInt())到它,它解決了問題,但由於某種原因,輸出文件的所有值爲零。總分,平均分數和通過的學生數量都爲零。 – beta4attack

+0

我可以推薦你學習使用調試器嗎?這將比問stackoverflow更有效率... – meriton

回答

0

當掃描器沒有(在這種情況下)不再有int讀取時拋出NoSuchElementException。

我不知道你的.txt文件的結構,所以如果你可以將它添加到你的問題將是很好的。

提供可能的解決將是做重置掃描儀的偏移量如下:

scan.close(); 
scan = new Scanner(f); 
for (int i = 0; i < 30; i++) { 
    for (int j = 0; j < 6; j++) { 
     subjects[i][j] = scan.nextInt(); 
    } 
} 

編輯:

此代碼爲我工作:

public static void main(String[] args) throws Exception { 
    Scanner scan = new Scanner(new FileInputStream(new File("file.txt"))); 
    PrintWriter p = new PrintWriter("Class_report.txt"); 
    int x = 0, num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0; 
    double avg1 = 0, avg2 = 0, avg3 = 0, avg4 = 0, avg5 = 0, avg6 = 0; 
    int[] total = new int[30]; 
    int[] number = new int[6]; 
    int[][] subjects = new int[30][6]; 
    double[] average = new double[6]; 
    for (int i = 0; i < 30; i++) { 
     for (int j = 0; j < 6; j++) { 
      x = x + scan.nextInt(); 
     } 
     total[i] = x; 
     x = 0; 
    } 

    scan = new Scanner(new FileInputStream(new File("file.txt"))); 
    for (int i = 0; i < 30; i++) { //Exception starts appearing from this line. 
     for (int j = 0; j < 6; j++) { 
      subjects[i][j] = scan.nextInt(); 
     } 
    } 
    for (int i = 0; i < 30; i++) { 
     avg1 = avg1 + (double) subjects[i][0]; 
     avg2 = avg2 + (double) subjects[i][1]; 
     avg3 = avg3 + (double) subjects[i][2]; 
     avg4 = avg4 + (double) subjects[i][3]; 
     avg5 = avg5 + (double) subjects[i][4]; 
     avg6 = avg6 + (double) subjects[i][5]; 
    } 
    average[0] = avg1/30; 
    average[1] = avg2/30; 
    average[2] = avg3/30; 
    average[3] = avg4/30; 
    average[4] = avg5/30; 
    average[5] = avg6/30; 
    for (int i = 0; i < 30; i++) { 
     if (subjects[i][0] >= 50) { 
      num1++; 
     } 
     if (subjects[i][1] >= 50) { 
      num2++; 
     } 
     if (subjects[i][2] >= 50) { 
      num3++; 
     } 
     if (subjects[i][3] >= 50) { 
      num4++; 
     } 
     if (subjects[i][4] >= 50) { 
      num5++; 
     } 
     if (subjects[i][5] >= 50) { 
      num6++; 
     } 
    } 
    number[0] = num1; 
    number[1] = num2; 
    number[2] = num3; 
    number[3] = num4; 
    number[4] = num5; 
    number[5] = num6; 
    scan = new Scanner(new FileInputStream(new File("file.txt"))); 
    for (int i = 0; i < 30; i++) { 
     for (int j = 0; j < 6; j++) { 
      x = x + scan.nextInt(); 
     } 
     total[i] = x; 
     x = 0; 
    } 
    for (int i = 0; i < 30; i++) { 
     p.println("Total score of student #" + (i + 1) + ": " + total[i]); 
    } 
    for (int i = 0; i < 6; i++) { 
     p.println("Number of passing student in subject#" + (i + 1) + ": " + number[i]); 
    } 
    for (int i = 0; i < 6; i++) { 
     p.println("Average score in subject#" + (i + 1) + ": " + average[i]); 
    } 
    p.close(); 
} 

正如我所說的,我只是通過創建一個新的實例來重置Scanner的指針。只需用您的文件路徑替換「file.txt」即可。

+0

哦,對不起!我添加了輸入文本文件的結構。對不起! – beta4attack

+0

非常感謝您的回答,它很有效! :D – beta4attack

0

那麼,你完全讀取所有數字到你的total。但是,你沒有什麼可讀的,但仍然嘗試。所以你不看所以0 s在number,subjectsaverage的所有單元格中。更何況,當你嘗試填充它時,你會覆蓋total0

因此......您必須關閉並重新打開Class_scores.txt,或者首先閱讀並存儲本地情況下推薦的地方。

一旦你讀完整個File,你將無法從頭開始閱讀。

+0

認識到從上面解決它的人的代碼。非常感謝您的回答! – beta4attack