简体   繁体   English

如何读取具有多种数据类型的文本文件并将它们存储在数组中?

[英]How to read a text file with multiple data types and store them in an Array?

I am trying to read a text file of various data and getting it stored into an Array, so that after receiving user input I can compare it to see if there are any matches inside the Array.我正在尝试读取各种数据的文本文件并将其存储到数组中,以便在接收到用户输入后我可以比较它以查看数组内是否有任何匹配项。

example text:示例文本:

1A 1st true false false 28.50 free  
2B 2nd false true false 25.00 free  
3C 3rd true true false 32.50 free

As you can see I'm dealing with Strings, booleans and a double.如您所见,我正在处理字符串、布尔值和双精度值。

I initially tried reading the file and storing each line as a String in a String Array but that didn't work when I then needed to compare the user input.我最初尝试读取文件并将每一行作为字符串存储在字符串数组中,但是当我需要比较用户输入时这不起作用。

So I then tried to create an Array and a sub-array like below:所以我然后尝试创建一个数组和一个子数组,如下所示:

    try {
        File read = new File("seats.txt");
        Scanner reader = new Scanner(read);
        while (reader.hasNextLine()) {
            String dataRead = reader.nextLine();
            
            String[] seatData = dataRead.split(" ");
            
            String seatID = seatData[0];
            String seatClass = seatData[1];
            boolean window = Boolean.parseBoolean(seatData[2]);
            boolean aisle = Boolean.parseBoolean(seatData[3]);
            boolean table = Boolean.parseBoolean(seatData[4]);
            double seatPrice = Double.parseDouble(seatData[5]);
            String seatFree = seatData[6];
            
            Seats info = new Seats(seatID, seatClass, window, aisle, table, seatPrice, seatFree);
            result = info;
            System.out.println(result); }
        
            reader.close();
            
    }catch (FileNotFoundException e) {
        System.out.println("An error has occurred.");
        e.printStackTrace(); }
    return result; 
    
}

Is this the right method or should I look at trying something else?这是正确的方法还是我应该尝试其他方法?

Any insight or advice would be much appreciated.任何见解或建议将不胜感激。 Thanks.谢谢。

You reading and parsing is good, but you're not saving anything, you keep overwriting result with the last info , so your method return only one Seats instance, the last one, use a List to easily add all the Seats you create您阅读和解析很好,但是您没有保存任何内容,您一直用最后一个info覆盖result ,因此您的方法仅返回一个Seats实例,最后一个,使用List轻松添加您创建的所有 Seats

With some improvements经过一些改进

static List<Seat> read() {
    List<Seat> results = new ArrayList<>();
    File read = new File("seats.txt");
    String dataRead;

    try (BufferedReader reader = new BufferedReader(new FileReader(read))) {
        while ((dataRead = reader.readLine()) != null) {
            String[] seatData = dataRead.split(" ");
            String seatID = seatData[0];
            String seatClass = seatData[1];
            boolean window = Boolean.parseBoolean(seatData[2]);
            boolean aisle = Boolean.parseBoolean(seatData[3]);
            boolean table = Boolean.parseBoolean(seatData[4]);
            double seatPrice = Double.parseDouble(seatData[5]);
            String seatFree = seatData[6];
            Seat info = new Seat(seatID, seatClass, window, aisle, table, seatPrice, seatFree);
            System.out.println(info);
            results.add(info);
        }
    } catch (FileNotFoundException e) {
        System.out.println("An error has occurred.");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return results;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将存储在文本文件中的数据读取到数组中,然后对该数据进行排序并存储 - How to read the data stored in a text file into an array, then sort that data and store it 从文本文件中读取整数并将它们存储到 Java 中的数组中 - Read integers from a text file and store them into an array in Java 如何从文本文件中读取单个字符并将它们存储到二维数组中? - How do I get individual characters from read in text file and store them into a 2D array? 如何从文件中读取行并将其存储到数组中 - How do I read lines from a file and store them into an array 如何读取整数并将其存储在Java中的数组中 - how to read integers and store them in an array in java 如何存储字节并将其读回数组 - How to store bytes and read them back to an array 如何在数组中存储多个值(具有不同类型)并打印出来? - How do I store multiple values (with different types) in an array and print them out? 在数组中存储多种数据类型 - Java - Store multiple data types in array - Java 从文本文件中读取名称/电话号码,并将其存储到Java中的数组中 - Read names/phone numbers from text file and store them into an array in java 如何从文本文件中读取并存储到java中的数组中 - How to read from a text file and store into an array in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM