8

How to store a text file in tables

 3 years ago
source link: https://www.codesd.com/item/how-to-store-a-text-file-in-tables.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to store a text file in tables

advertisements

Hey guys so basically I have a "classroom.txt" file that has the the name of the professor along with the room capacity and actual number of students on the first line. After the first line is the name of the students, male or female, ID number, and age. IE

John Doe, 50, 25
David Clark, M, 100, 17
Betty Johnson, F, 101, 17
Mark Jones, M, 102, 18

basically I want to store John Doe, 50, 25 in an array List of Teachers and the rest in an array list of students.

try{ read = new Scanner(new File("classrom.txt"));
while(read.hasNextLine())
{
 //this is where I'm stuck to only read the first line into teacher arraylist
//and the rest into students
}
catch(Exception e)
System.out.println("File Not Found"!);


Since only the first line contains the teacher data, read the file once outside the loop. Within the loop, you can continue reading & adding into the student's ArrayList:

if(read.hasNextLine()){
    String teacherData = read.nextLine();
    teacherArrList.add(teacherData);
}
while(read.hasNextLine()){
    String studentData = read.nextLine();
    studentArrList.add(studentData);
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK