1. Member
Join Date
Apr 2013
Posts
15
Rep Power
0

## Sorting help

I'm trying to write a program that generates 100 random number, prints them, then sorts them, then prints out the sorted list with ten numbers in each line. I cannot figure out what is wrong with my code. Anyone have any ideas? A detailed explanation would be awesome as I am new to this. This is what I have so far.

Java Code:
public class Main {

public static void main(String[]Args)
{
int [] b;
b = new int[100];
printArray(b);
Sort(b);
printArray(b);
}

public static void loadArray(int[] b) {
for(int i = 0; i < b.length; i++){
b[i]= (int)((Math.random())*100)+1;{

}
}

}

public static void printArray(int[] b){
System.out.println();
for(int i = 0; i < b.length; i++){
System.out.print(b[i]);
if((i + 1)%10 == 0)
System.out.println();
}
}

public static void Sort(int [] b)
{
int Temp = 0;
for (int i = 0; i < b.length-1; i++)
{
for (int j = i + 1; j < b.length; j++)
{
if (b[i]> b[j])
{
Temp = b[i];
b[i] = b[j];
b[j] = Temp;
}
}
}

}

}
And here is an example of what it prints out right now.

42629035334048469325
21775561975248917456
9340882010067304331
5542393117952289996
3775521286392295798
83166760847191818556
696337363376546310037
99637451574393191
54155896825250881413
4479358294746523

1244455688
9111213141516202123
23252829293031313333
35363737373739394040
42434546474848505252
52525454545556565757
58586061626363676769
71747475767779818283
84858688909191919393
9393969696979899100100

As you can see all of the lines have more than 10 numbers in them except for the first line of the sorted list, which is also the only line that is sorted. Any help would be appreciated, thanks.

2. ## Re: Sorting help

Add some spaces in between the numbers you're printing.

3. ## Re: Sorting help

Crossposted: Sorting question

4. Member
Join Date
Apr 2013
Posts
15
Rep Power
0

## Re: Sorting help

Originally Posted by KevinWorkman
Add some spaces in between the numbers you're printing.
What do you mean? Wouldn't there still be more than 100 numbers in each list?

5. ## Re: Sorting help

Originally Posted by clerd
What do you mean? Wouldn't there still be more than 100 numbers in each list?
You print this out:

9393969696979899100100

93 93 96 96 96 97 98 99 100 100

6. Member
Join Date
Apr 2013
Posts
15
Rep Power
0

## Re: Sorting help

Originally Posted by KevinWorkman
You print this out:

9393969696979899100100

93 93 96 96 96 97 98 99 100 100
Omg derp haha thanks

7. Member
Join Date
Apr 2013
Posts
15
Rep Power
0

## Re: Sorting help

Originally Posted by KevinWorkman
You print this out:

9393969696979899100100

93 93 96 96 96 97 98 99 100 100
I can't think of any way to do that :[

8. ## Re: Sorting help

No way at all? What have you tried? You know how to print out a single number. Why can't you print out a single space?

9. ## Re: Sorting help

Originally Posted by KevinWorkman
Crossposted: Sorting question
Another: Sorting question (Beginning Java forum at JavaRanch)

db

10. ## Re: Sorting help

Aaand I'm done. Turns out I wasted my time giving you help you already received. This is exactly why crossposting is considered so rude.

11. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,739
Rep Power
10

## Re: Sorting help

Actually, my understanding is that cross posting is fine as long as the OP let's you know.

Regards,
Jim

12. ## Re: Sorting help

Originally Posted by jim829
Actually, my understanding is that cross posting is fine as long as the OP let's you know.

Regards,
Jim
Exactly. The OP did *not* let us know, even when I asked him to link between crossposts to avoid exactly what happened. It's not a big deal, but it does get annoying since I mostly only answer questions while my code compiles!

13. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Sorting help

Originally Posted by KevinWorkman
It's not a big deal, but it does get annoying since I mostly only answer questions while my code compiles!
Ha! I answer questions when my code doesn't compile. Guess why I spam so much!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•