2016-11-17 56 views
-1
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Scanner; 

public class Lab8 {  
public static void main(String[] args) throws IOException { 

    String line, filename = "input.txt"; 
    FileReader fp = new FileReader("input.txt"); 
    Scanner scan = new Scanner(fp); 
    while(scan.hasNextLine()) 
    { 
     line=scan.nextLine(); 
     System.out.println(line); 

    } 
    int [][] matrix = new int [3][5]; 

    for(int i = 0; i<3; i++) 
    { 
     for (int j = 0; j<5; j++) 
     { 

      matrix[i][j] = Integer.parseInt(scan.nextLine()); 
     } 
    } 

    for(int i = 0; i<3; i++) 
    { 
     //declare a sum variable and initialize to zero 
     int sum = 0; 
     for(int j = 0; j<5; j++) 
     { 

      sum +=scan.nextInt(sum); 
     } 
     //Compute the average of j-th quiz for all students 
     //Print the sum of all quizzes for the i-th student 
     System.out.println("Total Quiz result for student 1 is "); 
     System.out.println("Total Quiz result for student 2 is "); 
     System.out.println("Total Quiz result for student 3 is "); 
     System.out.println("Total Quiz result for student 4 is "); 
    } 


    for(int j=0; j<5; j++) 
    { 

     int sum=0; 

     for(int i=0; i<3; i++) 
     { 
      //update sum of j-th quiz 
      //by adding the score of i-th student 

     } 
     //Compute the average of j-th quiz for all students 
     //print the average corrected to two decimal places 
     System.out.println("Average of Quiz 1 is "); 
     System.out.println("Average of Quiz 2 is "); 
     System.out.println("Average of Quiz 3 is "); 
     System.out.println("Average of Quiz 4 is "); 
     System.out.println("Average of Quiz 5 is "); 

    } scan.close(); 
} 

} 

在我開始之前,只想指出我是java編碼的新手。我從這個得到的唯一輸出是從input.txt中文件中的內容:10 然後錯誤提示:線程「main」中的異常java.util.NoSuchElementException:找不到行。任何幫助是極大的讚賞。以下代碼的輸出應該如下所示:我試圖從input.txt文件讀取內容,將它們存儲在二維數組中,打印數組內容

用戶輸入從文本文件中讀取 學生1的總測驗結果是44學生2的總測驗結果是42學生3的總測驗結果是40測驗的平均值圖1是7.67 平均測驗2的是8.00平均測驗3的是9.00平均測驗4的是8.33平均測驗5的是9.00

+0

內容如何存儲?每行的數字或一行中的所有數字?我認爲這是所有的數字都在一行。我對嗎?或者你可以發佈文件的內容嗎? – SkrewEverything

+0

內容每行存儲1個數字。 – Camkin

+0

你能發佈文件的內容嗎? –

回答

0

的問題是使用的是Scanner對象多次讀取來自文件的輸入。當您使用nextInt()nextLine()時,指針向前移動並達到EOF。因此,重置指針只需關閉文件資源並重新打開即可重新讀取。

我編輯您的代碼:

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

public class Lab8 {  
public static void main(String[] args) throws IOException { 

    String line, filename = "input.txt"; 
    FileReader fp = new FileReader("input.txt"); 
    Scanner scan = new Scanner(fp); 
    /*while(scan.hasNextLine()) // -> Instead of printing here, print in the for loop below 
    { 
     line=scan.nextLine(); 
     System.out.println(line); 

    }*/ 
    int [][] matrix = new int [3][5]; 

    for(int i = 0; i<3; i++) 
    { 
     for (int j = 0; j<5; j++) 
     { 

      line=scan.nextLine(); 
      System.out.println(line); // print here. 
      matrix[i][j] = Integer.parseInt(line); 
     } 
    } 
    scan.close();      // close the scanner 
    fp.close();      // close the file reader 
    fp = new FileReader("input.txt"); // reopen file reader 
    scan = new Scanner(fp);   // reopen scanner 

    for(int i = 0; i<3; i++) 
    { 
     //declare a sum variable and initialize to zero 
     int sum = 0; 
     for(int j = 0; j<5; j++) 
     { 
      sum +=scan.nextInt(); 
     } 
     //Compute the average of j-th quiz for all students 
     //Print the sum of all quizzes for the i-th student 
     System.out.println("Total Quiz result for student 1 is "); 
     System.out.println("Total Quiz result for student 2 is "); 
     System.out.println("Total Quiz result for student 3 is "); 
     System.out.println("Total Quiz result for student 4 is "); 
    } 


    for(int j=0; j<5; j++) 
    { 

     int sum=0; 

     for(int i=0; i<3; i++) 
     { 
      //update sum of j-th quiz 
      //by adding the score of i-th student 

     } 
     //Compute the average of j-th quiz for all students 
     //print the average corrected to two decimal places 
     System.out.println("Average of Quiz 1 is "); 
     System.out.println("Average of Quiz 2 is "); 
     System.out.println("Average of Quiz 3 is "); 
     System.out.println("Average of Quiz 4 is "); 
     System.out.println("Average of Quiz 5 is "); 

    } scan.close(); 
} 

} 

關閉Scanner對象是不夠的。您必須關閉FileReader對象才能重置文件中的指針。

我希望它對你有所幫助。