我需要帮助从输入文件中拉出包含空格的字符串

时间:2016-02-10 14:42:53

标签: java function arraylist tabs tab-delimited-text

我正在创建一个程序,它从输入文件中获取学生记录并将它们存储在arraylist中。我抓住了学生证,姓名和三个考试成绩。每个项目由制表符分隔。我需要帮助抓住学生的名字作为一个整体。如果我像这样运行它会让人感到困惑,因为有些名字有姓,名和中间名。为了详细阐述我的主要问题,我试图获取名称I.E. J A Singleton退出文件并进入我的arraylist。我只是抓住了第一个名字,然后是姓氏并将它们组合在一起,但它并不总是有效,因为它们中的一些有三个不同的东西可以抓住,如上面的例子。有没有办法只抓取整个名称,因为文件中名称前后有一个标签?

public class Main {

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);
    }
}

1 个答案:

答案 0 :(得分:0)

使用String类拆分函数 S.split(&#34; \吨&#34); 这将返回基于tab分离的源数组 你可以在那里了解这一点 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;
    }