954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sorting items on a text file in java

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

nrue
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

nrue
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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;
        }
      }
    }
    }
nrue
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: