How do I properly bubble sort through a text file by assigning the values to an array.
In the code below I tried to assign the values from the text file to a string while there is still something to fetch. Then I used a for loop to assign the one that I have fetch to the array.

ttry{
 int i;
 String ss;
   FileReader fr;
      fr = new FileReader (new File("F:\\players.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);

}

int xh=0;

bubble_srt(player_score, player_score[xh]);
System.out.println(player_name[xh] + " " +player_score[xh]);
    
  }
      
  }catch(Exception e){}

I get this output:

a 5
b 10
x 4


from the players.txt, which looks like this:

1 a 5
2 b 10
5 x 4
7 h 20

please help

Recommended Answers

All 5 Replies

Can you ask specific questions about your problem?

What do the values you labeled as output and players.txt represent?
The output part has 3 lines with 2 tokens on each line, the players.txt part has 4 lines with 3 tokens on each line. What is the relationship between these two?

Can you ask specific questions about your problem?

What do the values you labeled as output and players.txt represent?
The output part has 3 lines with 2 tokens on each line, the players.txt part has 4 lines with 3 tokens on each line. What is the relationship between these two?

The relationship?I'm basically just extracting the parts that I need from the text file.
What I want to do is to sort the rightmost values, which is 5, 10, and 4. So that it becomes 10 5, 4.

Where in your code do you do the sort?
Can you explain what you are doing in the for loop in the code you posted?
And also why do you call sort before all the data is read in?

Where in your code do you do the sort?
Can you explain what you are doing in the for loop in the code you posted?
And also why do you call sort before all the data is read in?

Here's the bubble sort method:

public  void bubble_srt( int a[], int n ){
    int i, j,t=0;
    for(i = 0; i < n; i++){
      for(j = 1; j < (n-i); j++){
        if(a[j-1] > a[j]){
          t = a[j-1];
          a[j-1]=a[j];
          a[j]=t;
        }
      }
    }
    }

Your last post answered the first question, but left off the answers for the other two.
I'll give them again.

Can you explain what you are doing in the for loop in the code you posted?
And also why do you call sort before all the data is read in?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.