• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting question  RSS feed

 
Jefferey Stevens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure they've got more than 10 numbers in? Notice that on line 15 you're generating numbers in the range 1-100. You aren't generating single digits. Add some punctuation, and the last line will appear as:

93,93,96,96,96,97,98,99,100,100

Ten numbers, sorted in ascending order.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say you need to slow down. it's MUCH easier if you focus on one thing at a time. Your task seems to have at least three distinct parts:

1) Generate an array with 100 random numbers in it
2) Print an array
3) Sort an array.

Now, you should note that each of the above are completely independent of each other. Pick one, and get it to work before you even THINK about getting anything else to work. I would probably start with the "print array" one first. The rational is that you can use it to test the other two items in the list. So...start by just creating an array with some dummy values. I would literally do something like

int [] testingArray = {1,2,3,4,5,6,7,8,9,10,11,12}

and then pass that to my printArray method. Get that method ROCK solid. test it with different size array, with positive and negative values, with empty arrays, with sorted and unsorted values, etc. Once you are SURE it works, ONLY THEN should you work on item 1 or 3. Then get IT to work before you work on the other.
 
Jefferey Stevens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've realized that the second list is sorted, there are just no spaces between the double digit numbers. I can't think of any way to add spaces between the numbers.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jefferey Stevens wrote:I've realized that the second list is sorted, there are just no spaces between the double digit numbers. I can't think of any way to add spaces between the numbers.

Just add System.out.print(" ") after each number.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jefferey Stevens wrote:I've realized that the second list is sorted, there are just no spaces between the double digit numbers.

That's what you were told on (at least) one of the cross posts.
http://www.java-forums.org/new-java/81219-sorting-help.html
http://www.javaprogrammingforums.com/whats-wrong-my-code/31189-sorting-question.html

In future, please BeForthrightWhenCrossPostingToOtherSites
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!