Home > Software design >  How to read a text file with multiple data types and store them in an Array?
How to read a text file with multiple data types and store them in an Array?

Time:12-13

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.

CodePudding user response:

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

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

CodePudding user response:

Use Jackson. https://cowtowncoder.medium.com/reading-csv-with-jackson-c4e74a15ddc1. See second last example.

  • Related