-2

I am solving a problem from Advent of Code, and trying to put the content of the input file into an arraylist, here's my code for that:

ArrayList<Integer> arrayList = new ArrayList<>();
    try (Scanner s = new Scanner(new File("input.txt")).useDelimiter(",")) {

        while (s.hasNext()) {
            int b = Integer.parseInt(s.next());
            arrayList.add(b);
        }

    }
    catch (FileNotFoundException e) {
        // Handle the potential exception
    }


    System.out.println(arrayList);

and when I run it, it does not print the arraylist. I can't understand why, could someone tell me what I did wrong?

7
  • Are you trying to print the values of the integers in the arrayList? If so, please see here: stackoverflow.com/questions/9265719/print-arraylist
    – Corv1nus
    Apr 11, 2020 at 2:16
  • 1
    If the only thing it prints is [] and nothing else, then 1) file is empty, or 2) file is not found and you're ignoring the exception.
    – Andreas
    Apr 11, 2020 at 2:21
  • Maybe you're suppressing an exception by not having any code in the catch block. Maybe your file is empty so the loop never executes.
    – D.B.
    Apr 11, 2020 at 2:21
  • 1
    Sure! If you get an exception you do nothing except swallow it, and you assume you have successfully read values (even though your file is probably not where you think it is). It would also be an excellent idea to learn how to step through your program with a debugger. Apr 11, 2020 at 2:22
  • I've tried to do the same thing with an arraylist of strings and I did not have that issue, it printed the arraylist with all the numbers in the file. but once I change it back to int arraylist it does not print. @D.B.
    – Drayden
    Apr 11, 2020 at 2:26

2 Answers 2

0

I used StringTokenizer and it works perfectly. I am not familiar with using Scanner to split items, so I converted it over into a StringTokenizer. Hope you're okay with that.

public static void main(String[] args) throws IOException {
        ArrayList<Integer> arrayList = new ArrayList<>();
        Scanner s = new Scanner(new File("input.in"));

        StringTokenizer st = new StringTokenizer(s.nextLine(), ",");

        while (st.hasMoreTokens()) {
            int b = Integer.parseInt(st.nextToken());
            arrayList.add(b);
        }
        s.close();

        System.out.println(arrayList);

}

This fills your ArrayList with the values you want

0

You can validate if you are able to read the file or not. Your code can be modifed something like this. Please check if it prints "File found". If not it means that file you are trying to read is not in classpath. You might want to refer https://mkyong.com/java/java-how-to-read-a-file/

...
File source = new File("input.txt");
        if(source.exists()) {
            System.out.println("File found");
        }
        try (Scanner s = new Scanner(source).useDelimiter(",")) {
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.