0

I have a file that has the following data:

19387,73616,11/10/2021,7268,8271
81716,16381,11/10/2021,2736,3715,7362,837
75464,27315,11/10/2021,,3545,7645,460
  • The format is: id1, id2, yyyy/mm/dd, price, id3.
  • There can be multiple id3s (some lines have more data).

The data types are: int, int, string, float, arraylist integer. So here, I only want to read id3 as an integer arraylist

My code so far:

Public static void main(String[] args){

  //variables
  int id1;
  int id2;
  String date;
  float price;
  ArrayList<Integer> id3List = new ArrayList<>();

  //check if file exists
  try {
      //get file
      File myFile = new File (“fileName.txt“);

      //pass file object to create scanner object
      Scanner inputFile = new Scanner ("myFile").useDelimiter(",|\\r|\\n");
      
      //read lines from file and store into respective variables
      try {
        While (inputFile.hasNextInt){
          id1 = inputFile.nextInt();
          id2 = inputFile.nextInt();
          date = inputFile.next();
          price = inputFile.nextFloat();
          id3.add(inputFile.nextInt());

          inputFile.nextLine(); //consume a line 

      } catch(InputMismatchException e){
        System.out.println("Bad data in file.");
      }
       System.out.println(id3);

       inputFile.close(); //close file

  } catch(FileNotFoundException e) {
     System.out.println("The file was not found.");
    }
}

The result i get only displays the first id3 and ignores the rest:

[8271, 3715, 7645]

Note: I will only be using a scanner for this, not buffered reader.

1
  • Is there a reason why you only want to use a scanner? Commented Oct 11, 2021 at 6:04

2 Answers 2

2

The first thing I would do would be to separate the parsing of the file from the parsing of the text.

That is, read a line of text from the file, then, using a seperate Scanner, parse that line.

This allows you to seperate the logic and workflow without potentially messing it up (but, that's me).

You also need to take into account that if the price is empty, you will have a dangling element, which should be read (and discard).

Finally, make use of hasNextInt to loop through the remaining tokens

For example...

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner("""
                                      19387,73616,11/10/2021,7268,8271
                                      81716,16381,11/10/2021,2736,3715,7362,837
                                      75464,27315,11/10/2021,,3545,7645,460
                                      """);
        while (scanner.hasNextLine()) {
            Scanner line = new Scanner(scanner.nextLine()).useDelimiter(",");
            int id1 = line.nextInt();
            int id2 = line.nextInt();
            String date =  line.next();

            float price = 0;
            if (line.hasNextFloat()) {
                price = line.nextFloat(); 
            } else {
                line.next();
            }

            System.out.println(id1);
            System.out.println(id2);
            System.out.println(date);
            System.out.println(price);

            List<Integer> otherIds = new ArrayList<>(25);
            while (line.hasNextInt()) {
                otherIds.add(line.nextInt());
            }

            System.out.println(otherIds);

            System.out.println();
        }
    }
}

This will print...

19387
73616
11/10/2021
7268.0
[8271]

81716
16381
11/10/2021
2736.0
[3715, 7362, 837]

75464
27315
11/10/2021
0.0
[3545, 7645, 460]
Sign up to request clarification or add additional context in comments.

Comments

-1

You’re very close to the actual solution. In this case inputFile.nextInt() will return NULL when no more integers can be found. This is why we can use a while loop to append to the array.

while(inputFile.nextInt())
{
     id3.add(inputFile.nextInt());
}

4 Comments

"inputFile.nextInt() will return NULL" - I'm pretty sure it doesn't, have a read of the JavaDocs. You are, however close to the idea
Sorry, you’re right. Didn't read the Docs. However, the idea of the while loop still holds.
Yes, the "idea" is correct, the implementation is not ;)
Yeah, i was thinking of using another while loop for the arraylist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.