Hey guys I am fairly new to java and I have this problem when trying to add numbers from a file called compact.txt into an array. Here is my code so far:
public void compactArray(){
try{
Scanner scan = new Scanner(new File("compact.txt"));
while(scan.hasNextInt()){
num++;
}
int [] a = new int[num];
Scanner in = new Scanner(new File("compact.txt"));
while(counter < num){
a[counter] = in.nextInt();
counter++;
}
System.out.println(Arrays.toString(a));
}catch(IOException bob){
bob.getMessage();
}
}
The problem with this code is that it never stops running. First my code reads the compact.txt and then counts the amount of numbers it has to figure out the size of the array. Then I make another scanner variable to add the numbers from compact.txt into the array. I use a counter variable as a way to stop when the desired amount of numbers are added into array a. I am not too sure what the problem is but it keeps on running and doesn't get to the line where it is supposed to print out the array. Can someone please help me. Thank you so much.