arrays - how to bubble sort through a text file in java -


how bubble sort through text file assigning values array. in code below tried assign values text file string while there still fetch. used loop assign 1 have fetch array. tried use bubble sort sort numbers have fetch highest lowest. 1 output:

5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5   10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10   4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4   20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20   100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0   78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78   12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12   29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29   

here's code:

     try{  int i;  string ss;    filereader fr;       fr = new filereader (new file("x:\\file.txt"));       bufferedreader br = new bufferedreader (fr);   while ((ss = br.readline()) != null) {     string[] sv = ss.split(" ");           string splayer_name=sv[1];         string s_player_score=sv[2];   for(int xy=0;xy<player_name.length;xy++){         player_name[xy]=splayer_name;         player_score[xy]=integer.parseint(s_player_score); }   bubble_srt(player_score, player_score.length);      for(i = 0; <player_score.length; i++)       system.out.print(player_score[i]+"  ");     system.out.println();       }    }catch(exception e){} 

please help,

update: asked file structure, referring one: file.txt, 1 i'm fetching rightmost number:

1 5 2 b 10 5 x 4 7 h 20 

  1. move loop & bubble_srt() call outside while loop. replacing entire contents on each iteration earlier answer has correctly pointed out. want sort after entire file contents read.
  2. since file can of size there no way initialize array beforehand unless want perform 2 reads 1 count , read array. better off using list. here's skeleton code:

    try {     int i;     string ss;     filereader fr;     fr = new filereader(new file("file.txt"));     bufferedreader br = new bufferedreader(fr);     int xy=0;     list<string> player_name = new arraylist<string>();     list<integer> player_score = new arraylist<integer>();   
    while ((ss = br.readline()) != null) {     string[] sv = ss.split(" ");           player_name.add(sv[1]);         player_score.add(integer.parseint(sv[2]));   }   bubble_srt(player_score); 
    } catch (exception e) { }

(modify bubble sort take list input)


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -