我试图从input.txt文件读取内容,将它们存储在二维数组中,打印数组内容
问题描述:
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
答
的问题是使用的是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
对象才能重置文件中的指针。
我希望它对你有所帮助。
内容如何存储?每行的数字或一行中的所有数字?我认为这是所有的数字都在一行。我对吗?或者你可以发布文件的内容吗? – SkrewEverything
内容每行存储1个数字。 – Camkin
你能发布文件的内容吗? –