简体   繁体   中英

I need help pulling a string with spaces in it from an input file

I am making a program that takes in student records from an input file and stores them in a arraylist. I am grabbing the student ID, Name, and three test scores. Each item is separated by tabs. I need help grabbing the student name out as one piece. It messes up if I run it like this because some of the names have a last name, first name, and middle initial. To elaborate on my main problem, I am trying to grab the name IE JA Singleton out of the file and into my arraylist. I was just grabbing the first name and then the last name and putting them together, but it doesn't always work because some of them have three different things to grab such as my example above. Is there someway to just grab the whole name since there is a tab before and after the name in the file?

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

Use String class split function like S.split("\\t"); This will return string array from source separated on the basis of tab You can learn about that at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html Here is the code you can try, a function to get student records from file file name is passed as parameter. Make sure you have tab between id, name, and each score.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM