0

I am taking data from a text file and putting it into an ArrayList. However the first line of the text file is not being printed out.

public static void secondMain() {
    BufferedReader reader;
    var lines = new ArrayList<String>();
    var rooms = new ArrayList<Room>();
    try {
        reader = new BufferedReader(new FileReader("rooms.txt"));
        String line = reader.readLine();
        while (line != null) {
            line = reader.readLine();
            lines.add(line);
        }
        
        reader.close();
        
        for (int i = 0; i < lines.size() - 1; i++) {
            String[] words = lines.get(i).split(" ");
            var room = new Room();
            room.RoomNumber = Integer.parseInt(words[0]);
            room.Type = (words[1]);
            room.Price = Double.parseDouble(words[2]);
            room.Bool1 = Boolean.parseBoolean(words[3]);
            room.Bool2 = Boolean.parseBoolean(words[4]);
            room.Availability = (words[5]);
            rooms.add(room);
            
        }



        for(int i = 0; i < rooms.size(); i++) {
            System.out.println(rooms.get(i).RoomNumber);
            System.out.println(rooms.get(i).Type);
            System.out.println(rooms.get(i).Price);
            System.out.println(rooms.get(i).Bool1);
            System.out.println(rooms.get(i).Bool2);
            System.out.println(rooms.get(i).Availability);
                            
        }

Apologies for the Image, it was the only way i could figure out how to show the formatting of the text file. Image of the text file

The current output is displaying room number 102 as the first room which obviously isn't correct.

If anyone could also help me figure out how to format my console output the same way as the text file that'd also be great. currently it is displaying each individual string/int etc. on a different line.

Thanks.

If you need any more information please just ask!

2 Answers 2

6

This has nothing to do with the ArrayList. You can reproduce the problem by replacing the lines.add(line) call with System.out.println(line) and you'll see that the first line is missing from the output. Look at your first call to readLine(), before the while loop. You test that the value is non-null... and that's all you do with it (comments mine):

String line = reader.readLine(); // Read the value...
while (line != null) {           // Test for it being non-null
    line = reader.readLine();    // Then ignore the value you've just tested,
                                 // by reading the next line.
    lines.add(line);
}

You then call readLine() again. Note that your list will always end with a null value (unless it's the first line read) because your loop effectively says "while the last entry I added to the list isn't null". The simplest fix is to swap the order of the statements within your loop:

String line = reader.readLine();
while (line != null) {
    lines.add(line);
    line = reader.readLine();
}

Now you're adding a line immediately after checking whether it's non-null, before reading the next line.

0
4

The problem is here:

String line = reader.readLine(); // you read the first line
while (line != null) {
    line = reader.readLine(); // read second line
    lines.add(line); // add second line
}

You read the first line, check if it's not null, and then read the second line right away, before you add it to the list. All you need to do is switch the order.

String line = reader.readLine();
while (line != null) {
    lines.add(line);
    line = reader.readLine();
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.