如何在Java中从.txt文件中提取信息?
我目前有一些代码可以接受控制台输入,但我需要让它识别一个名称和测试分数列表。 我目前拥有的代码:如何在Java中从.txt文件中提取信息?,java,text,Java,Text,我目前有一些代码可以接受控制台输入,但我需要让它识别一个名称和测试分数列表。 我目前拥有的代码: import java.io.*; import utils.io.*; class student { String name; double sResult[]; int result; double sum; void getdata() { System.out.println("Enter name:"); Name = (s
import java.io.*;
import utils.io.*;
class student
{
String name;
double sResult[];
int result;
double sum;
void getdata()
{
System.out.println("Enter name:");
Name = (string) system.in.read();
int index=0;
for (int counter=1; counter<=5; counter++)
{
System.out.println("Enter result for subject"+counter+":");
sResult[count] = (double) system.in.read();
}
}
public static void CalculateAverage()
{
sum = 0;
for (int i=1;i<=5;i++)
{
sum += sResult[i];
}
return (int) Math.round(sum/(values.length-1));
}
Public static char calculateGrade()
{
result=sum;
if (result>=0 && result <=59)
{
return ('F');
}
else
if (result >=60 && result<=69)
{
return ('E');
}
else
if (result>=0 && result<79)
{
return ('D');
}
else
if (result >=70 && result<=79)
{
return ('C');
}
else
if (result>=80 && result<=89)
{
return ('B');
}
else
if (result>=90 && result<=100)
{
return ('A');
}
}
}
文本文档格式为name score1 score2 score3 score4 score5
我只需要知道如何导入值的帮助,然后我就可以知道如何使用PrintWriter重写.txt
提前感谢您的帮助 首先,变量名不应以大写字母开头,即:
string Name;
只有类名应该以大写字母开头
另外,我甚至不确定你的代码是否编译。无论在哪里,system.out.println都应该是system.out.println。请注意,System是一个类,因此第一个字母是大写字母
要读取数据,可以使用BufferedReader;要写入数据,可以使用BufferedWriter。
用法:
由于您正在以以下格式阅读:
姓名得分1分2分3分4分5分
您可以使用一些字符串函数来获取每个字段
String inputLine = in.readLine();
String [] fields = inputLine.split(" "); // Splits at the space
System.out.println(fields[0]); //prints out name
System.out.println(fields[1]); //print out score1
...
下面是如何使用扫描仪
要写回文件,您可以检查Davy的答案并使用缓冲写入器。请注意,write方法的工作原理与System.out.println类似,但您需要自己打印\n才能转到新行。此代码甚至可以编译!你试过BufferedReader吗?您想在这里导入什么import utils.io.*;?Java中有多种读取文本文件的方法。一种是在FileInputStream上使用BufferedReader。你也可以使用java.util.Scanner,或者如果你想要最新的东西,你可以用新的Files类来学习java nio2。我正在尝试使用Scanner。我很确定这就是我想要的。我一试就会给你回复。是的,split方法来自String类。看:谢谢!这会有帮助的!
public static void main(String... args) throws IOException {
Scanner s = new Scanner(new BufferedReader(new FileReader("textfile.txt")));
while(s.hasNext()){
Student student = new Student(s.next(), s.nextDouble(), s.nextDouble(),
s.nextDouble(), s.nextDouble(), s.nextDouble() ));
// Do something with student
s.next() // consume endline
}
}
String inputLine = in.readLine();
String [] fields = inputLine.split(" "); // Splits at the space
System.out.println(fields[0]); //prints out name
System.out.println(fields[1]); //print out score1
...
Scanner inputFile = new Scanner(new File("inputfile.txt"));
while(inputFile.hasNext()){ //if there is still something to read
String name = inputFile.next();
int score1 = inputFile.nextInt();
....
int score5 = inputFile.nextInt();
//Do something here with the scores
inputFile.next(); //Read the new line character and prepare for the next iteration
}
public static void main(String... args) throws IOException {
Scanner s = new Scanner(new BufferedReader(new FileReader("textfile.txt")));
while(s.hasNext()){
Student student = new Student(s.next(), s.nextDouble(), s.nextDouble(),
s.nextDouble(), s.nextDouble(), s.nextDouble() ));
// Do something with student
s.next() // consume endline
}
}