0

I've been writing a program to model a train ticket booking service although the first task of all is too read in the data from a .txt file and then store it too an array of which I can refer back to for other processes like booking and cancelling bookings. I've read the file in to the seats array although however when I go to access it by index value it will only give me the actual value of the last index of the array.

//How can I access the array value that I want and why does 
//system.out.println(seats[0]); give me 6D when it should give me 1A?

public static final seat[] seats = new seat[18];
public static Scanner scanner = new Scanner(System.in);
private static Scanner file;

public static void main(String[] args) throws FileNotFoundException {       
    initSeats();
    System.out.println(seats[0].seatName);
    //prints 6D - the seatName of the last index

    }

public static void initSeats() throws FileNotFoundException {

    file = new Scanner(new FileReader("seats (1).txt")); 
    int i = 0;

    while(file.hasNext()) {

        String name = file.next();
        String seatType = file.next();
        Boolean window = Boolean.parseBoolean(file.next());
        Boolean aisle = Boolean.parseBoolean(file.next());
        Boolean table = Boolean.parseBoolean(file.next());
        Double seatPrice = Double.parseDouble(file.next());
        String email = file.next();

        seats[i] = new seat(name, seatType, window, aisle, table, seatPrice, email);
        i++;

       }
    }
}

Also here is the txt file of which i'm reading from

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

Here is my seat class

public class seat {

public static String seatName, seatType, email;
public static boolean window, aisle, table;
public static double seatPrice;


public seat(String seatName, String seatType, boolean window, boolean aisle, 
    boolean table, double seatPrice, String email) {

    this.seatName = seatName;
    this.seatType = seatType; 
    this.email = email;
    this.window = window;
    this.aisle = aisle;
    this.table = table;
    this.seatPrice = seatPrice;

    }
public String toString() {

    String result = "\nSeatName = " + seatName + "\nseatType = " + seatType 
    + "\nEmail = " + email + "\nwindow = " + window + "\naisle = " + aisle + 
    "\ntable = " + table + "\nseatPrice = " + seatPrice;
    return result;
}

1 Answers1

-1

Use either StringTokenizer or split API, something like below

BufferedReader br = null;
FileReader fr = null;
int i = 0;

try {

    //br = new BufferedReader(new FileReader(FILENAME));
    fr = new FileReader(FILENAME);
    br = new BufferedReader(fr);

    String sCurrentLine;

    while ((sCurrentLine = br.readLine()) != null) {
        // Split the current string 
        StrinTokenizer token = new StringTokenizer(sCurrentLine);
        while(token.hasNext()) {
            String seatType = token.next();
            Boolean window = Boolean.parseBoolean(token.next());
            Boolean aisle = Boolean.parseBoolean(token.next());
            Boolean table = Boolean.parseBoolean(token.next());
            Double seatPrice = Double.parseDouble(token.next());
            String email = token.next();
            seats[i] = new seat(name, seatType, window, aisle, table, seatPrice, email);
            i++;
        }
    }

} catch (IOException e) {

    e.printStackTrace();

} 
Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25