我正在制作一个程序,该程序从输入文件中获取学生记录,并将其存储在arraylist中。我正在获取学生证,姓名和三个考试成绩。每个项目均由标签分开。我需要帮助将学生姓名一并列出。如果我这样运行它会搞砸,因为某些名称具有姓氏,名字和中间的缩写。为了详细说明我的主要问题,我正在尝试从文件中提取名称IE JA 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);
}
}
使用String类拆分函数,例如S.split(“ \ t”); 这将从基于选项卡分隔的源返回字符串数组。您可以在https://docs.oracle.com/javase/7/docs/api/java/lang/String.html上了解有关此内容的代码。试试,从文件文件名获取学生记录的函数作为参数传递。确保在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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句