**This is my first post so don't know if I did this right. But I have to read a file and then put the list in order from smallest to largest by the population. All I get is Alabama and that shows up only one time. I think my problem is from the "for" statement but I am not sure. It could also be from the "return" statement. The file is set up like this
Alabama,4779736
Alaska,7102313**
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Inorder {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
PrintWriter prw = new PrintWriter("outfile.txt");
File f = new File("census2010.txt");
if(!f.exists()) {
System.out.println( "f does not exist ");
}
Scanner infile = new Scanner(f);
infile.useDelimiter ("[\t|,|\n|\r]+");
final int MAX = 50;
int [] myarray = new int [MAX];
String[] statearray = new String[MAX];
int fillsize;
fillsize = fillarray (myarray, statearray, infile);
printarray (myarray, fillsize, prw);
sortarray(myarray, statearray, fillsize);
}
public static int fillarray (int[] num, String[] states, Scanner infile){
for( int count = 0; count < 50; count++){
int retcnt = 0;
int pop;
String state;
state = infile.next();
pop = infile.nextInt();
System.out.println(state + " " + pop + " ");
states[retcnt] = state;
num[retcnt] = pop;
retcnt++;
return (retcnt); }
}
public static void printarray (int[] num, int fillsize, PrintWriter prw){
for (int counts = 0; counts < fillsize ; counts++){
System.out.println("For the position ["+counts+"] the value is " + num[counts]);
prw.println("For the position ["+counts+"] the value is " + num[counts]);
}
return;
}
public static void sortarray(int[] poparray, String[] statearray, int fillsize){
for( int fill = 0; fill < fillsize -1; fill = fill+1){
for ( int compare = fill+1; compare < fillsize; compare++){
if( poparray[compare] < poparray[fill]){
int poptemp = poparray[fill];
poparray[fill] = poparray[compare];
poparray[compare] = poptemp;
// do I need something here?
String statetemp = statearray[fill];
statearray[fill] = statearray[compare];
statearray[compare] = statetemp;
}
}
}
}
}
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.
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;