Java 我需要帮助从输入文件中提取包含空格的字符串
我正在制作一个程序,从输入文件中接收学生记录并将其存储在arraylist中。我正在抓取学生ID、姓名和三个考试分数。每个项目由选项卡分隔。我需要帮助把学生的名字完整地写出来。如果我像这样运行它,它会搞砸,因为有些名字有姓、名和中间的首字母。为了详细说明我的主要问题,我试图从文件中获取名称,即J A Singleton,并将其放入我的arraylist中。我只是把名字和姓氏放在一起,但这并不总是有效,因为有些人有三种不同的东西需要抓取,比如我上面的例子。由于文件中的名称前后都有一个选项卡,是否有办法获取整个名称 公共班机{Java 我需要帮助从输入文件中提取包含空格的字符串,java,function,arraylist,tabs,tab-delimited-text,Java,Function,Arraylist,Tabs,Tab Delimited Text,我正在制作一个程序,从输入文件中接收学生记录并将其存储在arraylist中。我正在抓取学生ID、姓名和三个考试分数。每个项目由选项卡分隔。我需要帮助把学生的名字完整地写出来。如果我像这样运行它,它会搞砸,因为有些名字有姓、名和中间的首字母。为了详细说明我的主要问题,我试图从文件中获取名称,即J A Singleton,并将其放入我的arraylist中。我只是把名字和姓氏放在一起,但这并不总是有效,因为有些人有三种不同的东西需要抓取,比如我上面的例子。由于文件中的名称前后都有一个选项卡,是否有
public static void main(String[] args) throws Exception {
String stdID;
int tScore1;
int tScore2;
int tScore3;
String sName;
String fName;
String lName;
Students workobj;
try{
//opening the file for input
FileInputStream istream = new FileInputStream("input.txt");
Scanner input = new Scanner(istream);
//creating an arraylist to store student objects
ArrayList<Students> AllStudents= new ArrayList<Students>();
while(input.hasNextLine()){
//first I will read the student id
stdID=input.next();
//remove later
System.out.println("stdEcho "+ stdID);
//next I will read the student name
fName= input.next();
lName=input.next();
sName=(fName+lName);
//remove later
System.out.println("NameEcho " + sName);
//next read in the test scores
tScore1=input.nextInt();
//remove later
System.out.println("Test01Echo " +tScore1);
tScore2=input.nextInt();
//remove later
System.out.println("Test02Echo " +tScore2);
tScore3=input.nextInt();
//remove later
System.out.println("Test03Echo " +tScore3);
//printing the record
System.out.println("Student ID: "+stdID + " Student Name: " + sName + " Test Score 1: " +tScore1
+ " Test Score 2: " + tScore2 + " Test Score 3: " + tScore3);
output.println("Student ID: "+stdID + " Student Name: " + sName + " Test Score 1: " +tScore1
+ " Test Score 2: " + tScore2 + " Test Score 3: " + tScore3);
//creating a student object
Students StudentRecord= new Students(stdID,sName,tScore1,tScore2,tScore3);
StudentRecord.listStudents();
//now store this in allstudents
AllStudents.add(StudentRecord);
}//end of while
//Now I will list the records
System.out.println("Getting Students from AllStudents Container");
for(int i=0;i<=AllStudents.size()-1;i++){
//retrieving the object
workobj=AllStudents.get(i);
workobj.listStudents();
}//end of for
System.out.println("This is the sorted values of Students from the AllStudents container");
sortLarge(AllStudents);
for(int i=0; i<=AllStudents.size()-1;i++){
workobj=AllStudents.get(i);
workobj.listStudents();
}
}//end of try
catch (FileNotFoundException e){
System.out.println("file not found");
System.err.println("File not found");
System.exit(11);
}// end catch
catch (InputMismatchException e){
System.out.println("Error in Reading File");
System.err.println("Error in Reading File");
System.exit(10);
}
finally {
output.close();
System.exit(2);
}
}
publicstaticvoidmain(字符串[]args)引发异常{
字符串stdID;
int tScore1;
int tScore2;
int tScore3;
弦圈套;
字符串fName;
字符串名称;
学生运动;
试一试{
//打开文件进行输入
FileInputStream istream=新的FileInputStream(“input.txt”);
扫描仪输入=新扫描仪(istream);
//创建用于存储学生对象的arraylist
ArrayList AllStudents=新建ArrayList();
while(input.hasNextLine()){
//首先我要读学生证
stdID=input.next();
//以后删除
System.out.println(“stdEcho”+stdID);
//接下来我会读学生的名字
fName=input.next();
lName=input.next();
sName=(fName+lName);
//以后删除
System.out.println(“namecho”+sName);
//下一步阅读考试成绩
tScore1=input.nextInt();
//以后删除
System.out.println(“Test01Echo”+tScore1);
tScore2=input.nextInt();
//以后删除
System.out.println(“Test02Echo”+tScore2);
tScore3=input.nextInt();
//以后删除
System.out.println(“Test03Echo”+tScore3);
//打印记录
System.out.println(“学生ID:+stdID+”学生姓名:+sName+”测试分数1:+tScore1
+“测试分数2:+tScore2+”测试分数3:+tScore3);
输出.println(“学生ID:+stdID+”学生姓名:“+sName+”测试分数1:+tScore1
+“测试分数2:+tScore2+”测试分数3:+tScore3);
//创建学生对象
学生记录=新生(stdID、sName、tScore1、tScore2、tScore3);
StudentRecord.listStudents();
//现在将此存储在所有学生中
所有学生。添加(学生记录);
}//结束
//现在我将列出记录
System.out.println(“从AllStudents容器获取学生”);
对于(int i=0;i使用字符串类拆分函数,如
S.split(“\t”);
这将返回源中的字符串数组,该数组根据制表符分隔
你可以在
下面是您可以尝试的代码,一个从文件中获取学生记录的函数
文件名作为参数传递。确保id、名称和每个分数之间有制表符
// Create list of All Records
public ArrayList<Students> getStudents(String fileName) throws Exception
{
System.out.println("2: Please Wait .... ");
String stdID;
int tScore1;
int tScore2;
int tScore3;
String sName;
//reading from "input.txt"
File data=new File(fileName);
FileReader reader=new FileReader(data.getAbsoluteFile());
BufferedReader breader= new BufferedReader(reader);
String eachrow;
ArrayList<Students> AllStudents= new ArrayList<Students>();
while((eachrow = breader.readLine()) != null)
{
String []r = eachrow.split("\t")
stdID = r[0];
sName = r[1];
tScore1 = r[2];
tScore2 = r[3];
tScore3 = r[4];
//creating a student object
Students StudentRecord= new Students(stdID,sName,tScore1,tScore2,tScore3);
StudentRecord.listStudents();
//now store this in allstudents
AllStudents.add(StudentRecord);
}
System.out.println("Student Records are Created Successfully.");
System.out.println("-----------------------------------------------------------------");
return AllStudents;
}
//创建所有记录的列表
公共ArrayList getStudents(字符串文件名)引发异常
{
System.out.println(“2:请稍候…”);
字符串stdID;
int tScore1;
int tScore2;
int tScore3;
弦圈套;
//从“input.txt”读取
文件数据=新文件(文件名);
FileReader=新的FileReader(data.getAbsoluteFile());
BufferedReader breader=新的BufferedReader(读卡器);
一串一串;
ArrayList AllStudents=新建ArrayList();
while((eachrow=breader.readLine())!=null)
{
字符串[]r=eachrow.split(“\t”)
stdID=r[0];
sName=r[1];
tScore1=r[2];
tScore2=r[3];
tScore3=r[4];
//创建学生对象
学生记录=新生(stdID、sName、tScore1、tScore2、tScore3);
StudentRecord.listStudents();
//现在将此存储在所有学生中
所有学生。添加(学生记录);
}
System.out.println(“学生记录创建成功”);
System.out.println(“-----------------------------------------------------------------------”;
归还所有学生;
}
您能详细说明一下您的具体问题吗?我对其进行了编辑,希望现在能更清楚一点。您能告诉我如何实现这一点吗?我正在尝试,并且不断出现错误。我现在已更新了我的答案检查,如果您自己出现错误,请删除所有错误。