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 9 10 8 7 7 8 9 8 10 6 7 8 9 10 然后错误提示:线程“main”中的异常java.util.NoSuchElementException:找不到行。任何帮助是极大的赞赏。以下代码的输出应如下所示:
从文本文件中读取用户输入 学生1的总测验结果为44总测验结果为学生2为42总测验结果为学生3为40平均测验1为7.67 测验2的平均值是8.00测验3的平均值是9.00测验4的平均值是8.33测验5的平均值是9.00
答案 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
对象才能重置文件中的指针。
我希望它对你有所帮助。