Array values from .txt file in java

I’m trying to get this code to print an array from a .txt file but the error: cannot find symbol for variable i pops up. I have no idea why

final int SIZE2 = 15;
int[] ages = new int[SIZE2];
Scanner finAges = new Scanner(new File("members.txt"));
for (int i = 0; i < SIZE2; i++)
  ages[i] = finAges.nextInt();
System.out.println(ages[i]);

Answer

Use curly brackets to enclose i in your loop’s scope:

for (int i = 0; i < SIZE2; i++) {
    ages[i] = finAges.nextInt();
    System.out.println(ages[i]);
}

Leave a Reply

Your email address will not be published. Required fields are marked *