如何在Java中访问单个数组值

时间:2017-11-29 00:19:29

标签: java arrays

我一直在编写一个程序来模拟火车票预订服务,虽然所有的第一项任务都是从.txt文件的数据中读取,然后存储它也是一个数组,我可以参考其他的预订和取消预订等流程。我已经将文件读入了seat数组,但是当我按索引值访问它时,它只会给我数组最后一个索引的实际值。

//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++;

       }
    }
}

此处还有我正在阅读的txt文件

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

这是我的座位类

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 个答案:

答案 0 :(得分:-1)

使用StringTokenizersplit API,如下所示

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();

}