It looks like you just need to move your return statement outside of the for loop.
public static int fillarray (int[] num, String[] states, Scanner infile){
for( int count = 0; count < 50; count++){
// ...
} // Finish *all* iterations of the loop, *then* return
return (retcnt);
}
By having a return
inside your loop, you only execute the first iteration and the method returns (preventing all other 49 iterations). You do this correctly in your printarray
method.
Edit:
As you mentioned, moving the return statement outside of your loop makes it so that retcnt
is no longer accessible. This is because you declare retcnt
inside the loop; if you declare retcnt
before the loop, you will have no problems.
int retcnt = 0;
for (//...) {
//...
}
// The variable retcnt is now accessible for the entire method scope,
// instead of just the loop block
return retcnt;