0

I want to get specific lines of data from a text file using arraylist. (more context: student details are stored in Student.txt. if i have to update a specific student, i want to get that student's line from text file into an arraylist in order to edit it)

Text file looks like this (ID, Name, Degreelevel, Email, Conatctno : courses);

A30, sarah, sarah@gmail.com, +64732 ; Computer Science, Cyber Security, Digital Media
A45,zaha, zaha@gmail.com, +3683: Software Engineering

My code prints all the data in the text file into the arraylist.

try {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    ArrayList<String> lines = new ArrayList<>();
    String line = reader.readLine(); 
    while (line != null)
    {
        lines.add(line);
        line = reader.readLine();
    } 
    reader.close();
    System.out.println(lines);            
}

current output:[ID, Name, Degreelevel, Email, Conatctno : courses, A30, sarah, sarah@gmail.com, +64732 ; Computer Science, Cyber Security, Digital Media, A45,zaha, zaha@gmail.com, +3683: Software Engineering]

So how can i search a specific student ID and input only that line into arraylist. Any hint helps. Thank you.

Madplay
  • 1,027
  • 1
  • 13
  • 25
aston
  • 5
  • 2
  • 2
    Instead of unconditionally adding the line to the `List`, extract the ID and `if` its the one you're looking for, then add it to the `List` – GBlodgett May 11 '19 at 01:11
  • For how to extract the ID look at [this](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) or the `substring` and `indexOf` methods of the `String` class – GBlodgett May 11 '19 at 01:12
  • @Blodgett. Thanks. It works. `try{ BufferedReader reader = new BufferedReader(new FileReader(file)); ArrayList lines = new ArrayList<>(); String line; while ((line = reader.readLine()) != null){ String[] y = line.split(","); if ((y[0].equals("A30"))){ lines.add(line); line = reader.readLine(); } } reader.close(); System.out.println(lines); }` – aston May 11 '19 at 01:44

1 Answers1

0

You have to go over each line until you find your matching id. This is one of the reasons why we typically like to store this type of information in databases rather than text files.

Simply add an if-statement that checks you got the right line before you add it to the arraylist.

A very simple way of doing this would be:

String myId = "A30";
while (line != null){
    if(line.startsWith(myId){
        lines.add(line);
        break;
    }
    line = reader.readLine();
} 

Normally when parsing text files one would split up each line using some data delimiter, e.g. line.split(","), rather than using startsWith. However, the data in your example is badly structured, using both colon, semi-colon and comma as delimiters. One common way of dealing with the problem that a string contains a delimiter is to encapsulate all string in quotation marks, and treat any special character found within quotation marks as a regular character. A semi-formalized structured format for data in text file is csv. Most languages (including Java) has libraries for parsing csv files.

Jonas Rosenqvist
  • 365
  • 5
  • 16