有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对文件中的多个数组进行排序。理顺各州人口

**这是我的第一篇帖子,所以不知道我是否做对了。但我必须读一个文件,然后按人口从最小到最大的顺序排列。我得到的只是阿拉巴马州,而那只出现过一次。我认为我的问题来自于“for”语句,但我不确定。它也可能来自“return”语句。文件是这样设置的

阿拉巴马州,4779736
阿拉斯加,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;
                                }
                            }
                        }
                    }
}

共 (1) 个答案

  1. # 1 楼答案

    看起来您只需要将return语句移到for循环之外

    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); 
     }
    

    通过在循环中有一个return,您只执行第一次迭代,方法返回(阻止所有其他49次迭代)。您可以在printarray方法中正确地执行此操作

    编辑:

    如前所述,将return语句移到循环之外会使retcnt不再可访问。这是因为您在循环内声明了retcnt;如果在循环之前声明retcnt,则不会有任何问题

    int retcnt = 0;
    for (//...) {
        //...
    }
    // The variable retcnt is now accessible for the entire method scope, 
    // instead of just the loop block
    return retcnt;