Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Sorting question

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.

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 question

This thread has been cross posted here:

Java Programming Forums Cross Posting Rules

3. ## Re: Sorting question

Originally Posted by KevinWorkman
This thread has been cross posted here:

Java Programming Forums Cross Posting Rules

The Problems With Cross Posting

I know, I'm sorry. I usually can figure it out faster if I get more advice though.

4. ## Re: Sorting question

I know, I'm sorry. I usually can figure it out faster if I get more advice though.

It might be faster for you, but it's a huge waste of our time, which is pretty rude considering we're doing this in our spare time for free.

5. ## Re: Sorting question

Originally Posted by KevinWorkman

It might be faster for you, but it's a huge waste of our time, which is pretty rude considering we're doing this in our spare time for free.
I'm sorry, I'll be more courteous next time.

I've found out that the list is sorted, there are just no spaces between the double digit numbers. I can't think of any way to add them.

6. ## Re: Sorting question

Hello.
You are not printing the numbers properly. How can someone like us know whether a number is 1-digit or 2-digit? Even though you are printing ten numbers in a row try to put some space in between every two numbers.
Then we can say if the numbers are really sorted or not. May be its sorted but we can't differentiate because of them being adjacent to each other.
If the problem is still not solved let us know.

Syed.