• Post Reply Bookmark Topic Watch Topic
  • New Topic

sorting array  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Please see my code below. I am not sure if my logic is correct, any assistance would be appreciated. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.



thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:Please see my code below. I am not sure if my logic is correct, any assistance would be appreciated. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.

Tip: when solving problems like this, try to break them down.

You have two issues:
1. Getting the input data.
2. Sorting the array.
of which, I suspect, you're more interested in the second than the first (at least, right now).

So write a program that simply sorts an array. You can add the other stuff later, and in the meantime it won't distract you.
And all you need to do that, to start with, is something like:
int[] a = new int[] { 167, 4, 23, 765, 0, -57 };
and then write your sort code.

A few other things:
1. Java class names should start with CAPITAL letters.
2. Line 9 is wrong because you don't have a variable called regnumber.
3. Line 12 is wrong because you're assigning an int to a regnumber.
4. If indeed you do want to sort an array of Regnumber's, then you need some way of comparing them.

HIH

Winston
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

please see sort code below. just need to get the right input data now.
 
Paweł Baczyński
Bartender
Posts: 2076
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that although syntax below is legal


you should use this one instead:

They mean exactly the same thig but second one is preffered.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:please see sort code below...

A few other points:
1. Your indenting and spacing is bad.

2. Don't use:
while (fixed == false) { ...
use:
while (!fixed) { ...
instead.
(thought I'd get that one in before Campbell )
You might also want to call it 'sorted' for clarity.

3. You can print out your entire array with:
System.out.println(Arrays.toString(regNumber));
(Arrays is a very good class to get to know).

4. Again, for clarity, you might want to write a swap() method; but it's not essential.

Other than that, it looks fine.

Winston
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback, it is really appreciated.

Carolin
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!