Blessed Rivalez
Blessed Rivalez

Reputation: 23

Passing integers from file to array

Someone Can explain me why this piece of code doesn't works? My array doesn't get filled with anything -.-

    Integer[] tab1 = new Integer[401];
    int[][] tab2 = new int[20][20];
    File fr;
    int i = 0, c = 0;
    fr = new File("problem11");
    Scanner sc;
    try {
        sc = new Scanner(fr);
        while (sc.hasNext()) {
            // System.out.printf("%d ", sc.nextInt());
            tab1[i] = sc.nextInt();

            i++;
            System.out.print(tab1[i]);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Upvotes: 0

Views: 55

Answers (1)

Reimeus
Reimeus

Reputation: 159754

You're displaying the next array element which hasnt been populated yet

i++;
System.out.print(tab1[i]);

should be

System.out.print(tab1[i++]);

Upvotes: 3

Related Questions