'Why is this code not running after my While loop in class constructor

Code following the while (mysc.hasNextLine()) is not running which leads to an error as arrays are not populated with data.

        try {
            File collectorFile = new File("items.csv");
            Scanner mysc = new Scanner(collectorFile);
            int count = 0;
            int count2 = 0;
            int position = 0;
            String[] records = new String[24];
            while (mysc.hasNextLine()) {
                
                String record = mysc.nextLine();
                records[count] = record;
                System.out.println(records[count]);
                count++;
                
            }
            System.out.println("hello thyere");
            for(String i : records) {
                String[] parts = i.split(",");
                if (count2 < 5) {
                    System.out.println("position:" + position + "count:" + count2);
                    Item test = new Item(Double.parseDouble(parts[0]), Integer.parseInt(parts[1]), parts[2],
                            Integer.parseInt(parts[3]));
                    a[position] = test;
                }
                if (count2 >= 5 && count <10) {
                    Item test = new Item(Double.parseDouble(parts[0]), Integer.parseInt(parts[1]), parts[2],
                            Integer.parseInt(parts[3]));
                    b[position] = test;
                }
                if (count2 >= 10 && count <15) {
                    Item test = new Item(Double.parseDouble(parts[0]), Integer.parseInt(parts[1]), parts[2],
                            Integer.parseInt(parts[3]));
                    c[position] = test;
                }
                if (count2 >= 15 && count <20) {
                    Item test = new Item(Double.parseDouble(parts[0]), Integer.parseInt(parts[1]), parts[2],
                            Integer.parseInt(parts[3]));
                    d[position] = test;
                }
                if (count2 >= 20 && count <25) {
                    Item test = new Item(Double.parseDouble(parts[0]), Integer.parseInt(parts[1]), parts[2],
                            Integer.parseInt(parts[3]));
                    e[position] = test;
                }
                
                count2++;
                if (position == 4) {
                    position = 0;
                }
            }

The output is

80,20,Walkers Ready Salted,0
80,20,Walkers Cheese and Onion,0
80,20,Walkers Cheese and Onion,0
80,20,Walkers Prawn Cocktail,0
80,20,Walkers Prawn Cocktail,0
80,20,Mars Bar,0
80,20,Mars Bar,0
80,20,CurlyWurly,0
80,20,CurlyWurly,0
80,20,Snickers Bar,0
80,20,Snickers Bar,0
80,20,Daim Bar,0
80,20,Daim Bar,0
80,20,Tayto Cheese and Onion,0
80,20,Tayto Cheese and Onion,0
80,20,Bounty bar,0
80,20,Tayto Salt and Vinegar,0
80,20,Tayto Salt and Vinegar,0
80,20,Tayto Prawn Cocktail,0
80,20,Tayto Prawn Cocktail,0
80,20,Haribo Star mix,0
80,20,Haribo Star mix,0
80,20,Haribo Tangfastics,0

The System.out.println("hello thyere"); is never reached.



Solution 1:[1]

I think you might be throwing a ArrayIndexOutOfBoundsexception, but since you're code is within a try-catch the exception might be stifled.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1