我试图从input.txt文件中读取内容,将它们存储在2D数组中,打印数组内容


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文件中的内容:1091087789810678910然后错误提示:线程"main"中出现异常java.util.NoSuchElementException: No line found。非常感谢任何帮助。下面代码的输出应该是这样的:

用户输入是从文本文件中读取的学生1的总测验结果是44学生2的总测验结果是42学生3的总测验结果是40学生1的平均测验结果是7.67测验二的平均值是8.00测验三的平均值是9.00测验四的平均值是8.33测验五的平均值是9.00

问题是您多次使用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对象来重置文件中的指针。

我希望这对你有帮助。

最新更新