Good evening, everyone!

I'm currently writing a game for a class, and I'm attempting to add a few extra features that aren't required.

The one that is giving me difficulty is the high scores.

I am saving the score and the name as a string. When a game ends, it saves the new score to a file. It then reads back in the entire file, parses it out line by line, takes the first 3 characters of each line (which is the score stored in a string format - the score is always 3 digits).

Here is where the difficulty comes in:

I convert the 3 char string back to an integer, and attempt to do a bubble sort on the list. It should (in theory anyway) then sort that array of strings (name and score together) that coincide with the scores that are being sorted.

Here is the section of code I am having trouble with:

String[] pirateInfo = daBloodyPirates.split("\n");
                for (int i = 0; i < pirateInfo.length; i++){
                String pirateScore = pirateInfo[i].substring(0,3);
                int tempScore1 = Integer.parseInt(pirateScore);
               
                    for(int j = 1; j < pirateInfo.length; j++){
                        String pirateScore2 = pirateInfo[j].substring(0,3);
                        int tempScore2 = Integer.parseInt(pirateScore2);
                       
                        for(int k = 0; k<pirateInfo.length-1; k++) {
                             if (tempScore2 > tempScore1){
                                int temp = tempScore1;
                                tempScore1 = tempScore2;
                                tempScore2 = temp;
                                String tempString = pirateInfo[i];
                                pirateInfo[i] = pirateInfo[j];
                                pirateInfo[j] = tempString;
                                System.out.println(pirateInfo[i]);
                                System.out.print("i is: ");
                                System.out.println(i);
                                System.out.print("j is: ");
                                System.out.println(j);
                                System.out.print("k is: ");
                                System.out.println(k);
                             }
                        }           
                    }  
                 }

And below is a sample of the ouput I get from the System.out.printlns:
400 Tester13

i is: 0
j is: 13
k is: 0
600 I should be on top

i is: 0
j is: 27
k is: 0
400 Tester16

i is: 1
j is: 15
k is: 0
400 Tester16

i is: 2
j is: 1
k is: 0
400 Tester16

i is: 3
j is: 2
k is: 0
400 Tester16

i is: 4
j is: 3
k is: 0
400 Tester16

i is: 5
j is: 4
k is: 0

-----------end of output-------------

k never gets incremented, and therefore the list never gets sorted.

Can anyone point out the logic error that I am simply not seeing?

Thank you

- Jim

Ah...after some much needed sleep, I realized the error myself.

Thank you all who took the time to read my post!

Happy coding!


- Jim

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.