Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1928866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:02:22+00:00 2026-05-17T07:02:22+00:00

How do I properly bubble sort through a text file by assigning the values

  • 0

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.
Then tried to use the bubble sort to hopefully sort the numbers that I have fetch from highest to lowest. But I get this one as an 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 my 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; i <player_score.length; i++)
      System.out.print(player_score[i]+"  ");
    System.out.println();




  }

  }catch(Exception e){}

Please help,

update:
someone asked about the file structure, are you referring to this one:
This is the file.txt, the one that I’m fetching is the rightmost number:

1 a 5
2 b 10
5 x 4
7 h 20
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T07:02:23+00:00Added an answer on May 17, 2026 at 7:02 am
    1. Move the for loop & the bubble_srt() call outside the while loop. You are replacing the entire contents on each iteration like what the earlier answer has correctly pointed out. You only want to sort AFTER the entire file contents are read.
    2. Since your file can be of any size there is no way to initialize the array beforehand unless you want to perform two reads one to get the count and then to read into the array. You would be better off using a List. Here’s a 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 your bubble sort to take a list input)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc compiles the following code without error. I'm creating a bubble sort function that
I have written a bubble sort algorithm to sort a linked list. I am
Update : Properly initialising string with char string[sizeof buffer - 1] has solved the
PHPUnit seems to execute properly (appropriate tests are executed & pass), but during code
I would like to display the content of an array in a Googlemap bubble.
I have a bubble generator that is mostly working, but is not properly clearing
I'm looking into using Domain Events to bubble information from operations occuring deep inside
I know bubble sort is probably not the fastest way to do this but
I'm trying to figure out the best way to bubble up PropertyChanged events from
<TextView android:id = @+id/txtBabbleListTitle android:layout_width = wrap_content android:layout_height = wrap_content android:textColor = #000000 android:text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.