I want to store the values from a .txt document into an array. I already tried this solutions and non of them worked. I want to store the values on an array because I will use that array in future methods to get the average, sum, etc.
This is what I came up with:
import java.util.Scanner;
import java.io.*;
public class example{
public static void main(String[] args)throws IOException{
Scanner input = new Scanner(new File("integers.txt"));
int counter = 0;
while(input.hasNext()){
int numbers = input.nextInt();
counter++;
}
int[] array = new int[counter];
for(int i =0; i < array.length; i++){
System.out.println(array[i]);
}
}
}
My output is: 0 0 0 0 0 0 0 0 0 0 0 0 0
And it should be the numbers on the file:
1 2 3 4 5 6 7 8 9 10 11 12 13
Any help will be much appreciated. Thank you in advance.