1

このプログラムを終了しましたが、問題が発生しました。これは、必要なものを出力していないことを意味します。国勢調査から州名と一緒に人口を取得し、最小の州から最大の州に並べ替えることになっています。プロジェクトを実行すると、人口の少ない州から人口の多い州まですべての州ではなく、アラバマ州とその人口が 50 回出力されます。国勢調査の例は次のとおりです...これらはそれぞれ別の行になります。

アラバマ、4779736
アラスカ、710231
アリゾナ、6392017

プログラムは次のとおりです。

    public static void main(String[] args) throws IOException {
    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) throws FileNotFoundException{

    int retcnt = 0;
    int pop;
    String state;
    state = infile.next();
    pop = infile.nextInt();
    for( int count = 0; count < 50; count++){
        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;
            }
        }
    }
}

私の問題はソート配列にあると思いますが、何が間違っていますか?

4

1 に答える 1