0

Below is the file I am reading the contents from to try and complete the task:

1A STD true false false 23.50 free
1B STD false true false 23.50 free
1D STD true true false 27.50 free
2A STD true false true 24.50 free
2B STD false true true 24.50 free
2D STD true true true 28.50 free
3A STD true false true 24.50 free
3B STD false true true 24.50 free
3D STD true true true 28.50 free
4A STD true false false 23.50 free
4B STD false true false 23.50 free
4D STD true true false 27.50 free
5A 1ST true true true 48.50 free
5C 1ST false true true 44.50 free
5D 1ST true false true 44.50 free
6A 1ST true true true 48.50 free
6C 1ST false true true 44.50 free
6D 1ST true false true 44.50 free

Now my objective is to store all these lines into separate array lists, e.g arrayList1 would be assigned to a value with the contents of the first line of the text file, and then these values are separated and are given the right data type e.g:

  • 1A is a string
  • true/false statements are assigned to booleans

Then after this reconstruct the file but the contents are assigned not all under one data type such as a string, but their own unique type instead.

int option;
File Seats = new File("seats.txt");

Scanner in = new Scanner(new FileReader(Seats));
String str;
ArrayList<String> list = new ArrayList<String>();
while(in.hasNextLine) { 

}

all I have tried so far but receive an error whenever I put anything into the while loop

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79

1 Answers1

0

I suppose what you need is a Seat class that represents a seat:

private static class Seat {
    private String seatNumber, seatClass;
    private boolean nearWindow, nearAisle, hasTable;
    private double seatPrice;
    private String email;

    private Seat(String seatNumber, String seatClass, boolean nearWindow, boolean nearAisle, boolean hasTable, double seatPrice, String email) {
        this.seatNumber = seatNumber;
        this.seatClass = seatClass;
        this.nearWindow = nearWindow;
        this.nearAisle = nearAisle;
        this.hasTable = hasTable;
        this.seatPrice = seatPrice;
        this.email = email;
    }
}

You can then map each line in your file to a Seat object, e.g.:

List<Seat> list = new ArrayList<>();
String[] strSplit;
Seat seat;
while(in.hasNextLine()) {
    strSplit = in.nextLine().split(" ");
    seat = new Seat(strSplit[0],
            strSplit[1],
            Boolean.parseBoolean(strSplit[2]),
            Boolean.parseBoolean(strSplit[3]),
            Boolean.parseBoolean(strSplit[4]),
            Double.parseDouble(strSplit[5]),
            strSplit[6]
    );
    list.add(seat);
}

On a sidenote, it is considered best practice to refer to objects by their interfaces, e.g.: List<String> list = new ArrayList<>(); instead of ArrayList<String> list = new ArrayList<String>();.

Christian S.
  • 877
  • 2
  • 9
  • 20