This site requires JavaScript, please enable it in your browser!
Greenfoot back
yemmaf21
yemmaf21 wrote ...

2021/5/18

I need to convert an array of integers into a string array after sorting it.

yemmaf21 yemmaf21

2021/5/18

#
public class Util  
{
   public static final String LEADERBOARD_FILE = "./saves/leaderboard.txt";
   public static String[] LoadLeaderboardLines() {
    String[] lines = LoadLines(LEADERBOARD_FILE);
    int[] lines;
    boolean sorted = false;
    int temp;
    while(!sorted) {
        sorted = true;
        for (int i = 0; i < lines.length - 1; i++) {
            if (lines[i] > lines[i+1]) {
                temp = lines[i];
                lines[i] = lines[i+1];
                lines[i+1] = temp;
                sorted = false;
            }
        }
    }
    String[] stringNums = new String[lines.length];
    int i = 0;
    while (i < lines.length) {
        stringNums[i] = String.valueOf(lines[i++]);
    }

    return stringNums;
   }
yemmaf21 yemmaf21

2021/5/18

#
public class Util  
{
   public static final String LEADERBOARD_FILE = "./saves/leaderboard.txt";
   public static String[] LoadLeaderboardLines() {
    int[] lines;
    boolean sorted = false;
    int temp;
    while(!sorted) {
        sorted = true;
        for (int i = 0; i < lines.length - 1; i++) {
            if (lines[i] > lines[i+1]) {
                temp = lines[i];
                lines[i] = lines[i+1];
                lines[i+1] = temp;
                sorted = false;
            }
        }
    }
    String[] stringNums = new String[lines.length];
    int i = 0;
    while (i < lines.length) {
        stringNums[i] = String.valueOf(lines[i++]);
    }
    String[] lines = LoadLines(LEADERBOARD_FILE);
    return stringNums;
   }

CODE SHOULD BE THIS
danpost danpost

2021/5/18

#
Line 5 declares an unassigned int array, which remains unassigned while you attempt to work with it. Don't you get a NullPointerException error traced to line 10 in your last code post above?
yemmaf21 yemmaf21

2021/5/19

#
no i dont i just get an error saying lines has already been defined in the method
danpost danpost

2021/5/19

#
yemmaf21 wrote...
no i dont i just get an error saying lines has already been defined in the method
What line do you get that on?
yemmaf21 yemmaf21

2021/5/23

#
line 24
rocket770 rocket770

2021/5/23

#
I don't know what other error you have or what you are trying to fix, but there seems to be a logic error regardless. I'm pretty sure there is a problem with line 22. Indexing lines++ means you are essentially skipping the first index and probably getting an index out of bounds error at the end along with null values for each index of stringNums. Try to replace it with
int i = 0;
    while (i < lines.length) {
        stringNums[i] = String.valueOf(lines[i]);
        i++;
    }
rocket770 rocket770

2021/5/23

#
Also, the reason the code is not compiling is that in line 5, you declare a variable named "lines" and you do the exact same in line 24. Try changing the name to something else. It also seems like once the code actually executes you'd get a null pointer exception as Danpost said. Do you do anything with the lines array before you try and sort it?
danpost danpost

2021/5/23

#
rocket770 wrote...
I'm pretty sure there is a problem with line 22. ... Try to replace it with << Code Omitted >>
Same thing. No problem there.
rocket770 wrote...
Also, the reason the code is not compiling is that in line 5, you declare a variable named "lines" and you do the exact same in line 24. Try changing the name to something else.
Yes. That is the current problem -- trying to declare another variable with the same name as one already declared.
It also seems like once the code actually executes you'd get a null pointer exception as Danpost said. Do you do anything with the lines array before you try and sort it?
I presume the int array (on line 5) is for scores. Where do the scores come from? Are they in the leaderboard file? How was the data (names and scores) put to the file? Don't they need to be read out and put to the arrays before you work with the arrays? Also, if you sort just the int array, do you not lose the one-to-one relation between scores and names?
You need to login to post a reply.