Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中访问单个数组值_Java_Arrays - Fatal编程技术网

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

如何在Java中访问单个数组值,java,arrays,Java,Arrays,我一直在编写一个程序来模拟火车票预订服务,尽管第一个任务是从.txt文件中读取数据,然后将其存储到一个数组中,我可以在预订和取消预订等其他过程中引用该数组。我已经将文件读入seats数组,但是当我通过索引值访问它时,它只会给出数组最后一个索引的实际值 //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

我一直在编写一个程序来模拟火车票预订服务,尽管第一个任务是从.txt文件中读取数据,然后将其存储到一个数组中,我可以在预订和取消预订等其他过程中引用该数组。我已经将文件读入seats数组,但是当我通过索引值访问它时,它只会给出数组最后一个索引的实际值

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

使用
StringTokenizer
split
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();

} 

我建议您使用hasNextLine(),然后使用nextLine().split(“”)提取每行的值。然后,只需使用for循环即可打印值。我可以按照我的方式提取值,这不是问题所在。我猜您确实使用了
私有静态字符串seatName
?@cricket\u 007猜得不错。Matthew,从Seat类中的所有字段中删除
static
。那我建议你好好读一读。公共的,私人的在这里都不重要<代码>静态是罪魁祸首