我正在教科书中练习,它要求我使用一个名为test.txt的文件。因此,我创建了所需的信息,并将其保存为test.txt。尝试使用inFile时,我总是收到错误消息。我的问题是.txt文件是否必须以某种方式格式化才能让eclipse能够读取?我正在做学生考试的平均档案。文件要求我放入文件中的firstName,lastName,test1,test2,test3,test4,test5,但它没有读取它。我是否必须在每个项目之间使用Tab键(使用MSWord)还是足够的空间? Eclipse无法读取在MSWord中创建的文件吗?如果不是,我应该在记事本中创建此文件吗?
我用保存为test.txt的MSWord编写了该文件,当该操作不起作用时,我在记事本中编辑了文件并以这种方式保存了文件,但仍然无法读取。然后,这使我怀疑是否是文件格式。首先,我尝试了firstName(tab)lastName(tab)test1(tab)...等等。然后我回过头来将其更改为firstName(space)lastName(space)test1(space),两种格式均未读取。
包装第3章;
导入java.io. ; 导入java.util。;
公共班级StudentGrade {
public static void main(String[] args) throws FileNotFoundException
{
//declare and initialize the variables
double test1, test2, test3, test4, test5;
double average;
String firstName;
String lastName;
Scanner inFile = new Scanner (new FileReader ("C:\\test.txt"));
PrintWriter outFile = new PrintWriter("C:\\testavg.out");
firstName = inFile.next();
lastName = inFile.next();
outFile.println("Student Name: " + firstName + "" + lastName);
test1 = inFile.nextDouble();
test2 = inFile.nextDouble();
test3 = inFile.nextDouble();
test4 = inFile.nextDouble();
test5 = inFile.nextDouble();
outFile.printf("Test Scores: %5.2f %5.2f %5.2f %5.2f %5.2 %n", test1, test2, test3, test4, test5);
average = (test1 + test2 + test3 + test4 + test5);
outFile.printf("Average test score: %5.2f %n", average);
inFile.close();
outFile.close();
}
}
我只需要它来读取文件,而且我不确定自己做错了什么。上面的代码来自课本,老师和书本都没有提供我需要的test.txt文件。
答案 0 :(得分:0)
不要使用Word。只需使用任何纯文本编辑器,它就可以工作。
答案 1 :(得分:0)
只需使用以下数据创建一个txt文件,然后将其放在“ C:\ test.txt”中
firstname
lastname
1.0
2.0
3.0
4.0
5.0
尝试并了解扫描仪的工作原理