java - Trouble sorting numbers from smallest to largest in an array -


this 1 of first posts on here not sure if posting correctly. need trying put population of states in order smallest largest comes separate file. program outputting states in alphabetical order. file set below.

expected input:

alabama,4779736 alaska,710231 arizona,6392017   

class attempts sort:

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 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) {          int retcnt = 0;         (int count = 0; count < 50; count++) {              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) {         (int counts = 0; counts < fillsize; counts++) {             system.out.println("for position [" + counts                     + "] value " + num[counts]);             prw.println("for position [" + counts + "] value "                     + num[counts]);         }         return;     }      public static void sortarray(int[] poparray, string[] statearray,             int fillsize) {          (int fill = 0; fill < fillsize - 1; fill = fill + 1) {              (int compare = fill + 1; compare < fillsize; compare++) {                  if (poparray[compare] < poparray[fill]) {                     int poptemp = poparray[fill];                     poparray[fill] = poparray[compare];                     poparray[compare] = poptemp;                     // need here?                     string statetemp = statearray[fill];                     statearray[fill] = statearray[compare];                     statearray[compare] = statetemp;                  }             }         }     } } 

your program already is sorting array in ascending order of population.

you not seeing it.

in main, print arrays , sort:

printarray (myarray, fillsize, prw); sortarray(myarray, statearray, fillsize); 

instead, try sorting , printing:

sortarray(myarray, statearray, fillsize); printarray (myarray, fillsize, prw); 

you'll see program correct.

tip: can use system.out.println(arrays.tostring(myarray)); print arrays easily.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -