# Sorting Numbers in Java

• (2 Pages)
• 1
• 2

## 15 Replies - 15009 Views - Last Post: 23 February 2009 - 07:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=87981&amp;s=726ee5eb226b2d366e6c14ee5d40c807&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 barsoum89

Reputation: 0
• Posts: 25
• Joined: 20-February 09

# Sorting Numbers in Java

Posted 20 February 2009 - 03:41 PM

```
public class FourNbrSort {

public static void main (String[] args)  {

String s = javax.swing.JOptionPane.showInputDialog("Enter Four Numbers: ");

int i = s.indexOf(" ");
int j = s.indexOf(",");
String an = s.substring(i+1,j);
double w = Double.parseDouble(an.trim());

int k = s.indexOf(",", j+1);
String bn = s.substring(j+1,k);
double x = Double.parseDouble(bn.trim());

int l = s.indexOf(",", k+1);
String cn = s.substring(k+1,l);
double y = Double.parseDouble(cn.trim());

int m = s.indexOf(",", l+1);
String dn = s.substring(l+1);
double z = Double.parseDouble(dn.trim());
if (w>x)

{
int temp;
w = temp;
w = x;
x = temp;
}

if (w>y)
{
int temp2;
w = temp2;
w = y;
y = temp2;
}

if (w>z)

{
int temp3;
w = temp3;
w = z;
z = temp3;
}

javax.swing.JOptionPane.showMessageDialog(null,"Input list : "+s+"\n"+
"Ordered list : " +);

```

i need to order these numbers using the IF-else statement and once i have that setup i really dont know how to show the output of the ordered list. Ignore the "Ordered list : " +) part of the code

if anyone could help me it would be great

Is This A Good Question/Topic? 0

## Replies To: Sorting Numbers in Java

### #2 Delta62

Reputation: 10
• Posts: 55
• Joined: 19-February 09

## Re: Sorting Numbers in Java

Posted 20 February 2009 - 03:55 PM

First off, some of these statements are not working correctly:
```if (w>x){
int temp;
w = temp;
w = x;
x = temp;
}
```

are you trying to say that temp = w instead of w = temp?

In assignment operations, the value to be assigned is read from the right side of the = sign, and assigned to the left hand side element.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Sorting Numbers in Java

Posted 20 February 2009 - 09:53 PM

Delta62 remark is accurate and justified and by the way the Code Snippets sections is full of sorts of all sorts

### #4 barsoum89

Reputation: 0
• Posts: 25
• Joined: 20-February 09

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 02:49 AM

Delta62, on 20 Feb, 2009 - 02:55 PM, said:

First off, some of these statements are not working correctly:
```if (w>x){
int temp;
w = temp;
w = x;
x = temp;
}
```

are you trying to say that temp = w instead of w = temp?

In assignment operations, the value to be assigned is read from the right side of the = sign, and assigned to the left hand side element.

when i put temp = w it gives me a build file error saying "possible loss of precision". am i on the right track though?

### #5 mostyfriedman

• The Algorithmi

Reputation: 728
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 02:51 AM

its because temp is int and w is double, if you try to assign a double to an int, there is loss in precision because double requires more memory than int, so to resolve this problem either change temp to double or change w to an int or you can cast w to an int but the decimals will be lost

This post has been edited by mostyfriedman: 21 February 2009 - 02:54 AM

### #6 barsoum89

Reputation: 0
• Posts: 25
• Joined: 20-February 09

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 10:38 AM

mostyfriedman, on 21 Feb, 2009 - 01:51 AM, said:

its because temp is int and w is double, if you try to assign a double to an int, there is loss in precision because double requires more memory than int, so to resolve this problem either change temp to double or change w to an int or you can cast w to an int but the decimals will be lost

thanks alot that helped sooooo much.

i'm really lost on how to sort them though from least to greatest. i know i'm supposed to use the if function but i'm not sure how to display it in a MessageDialog. for sorting the data does the if function above look right?

### #7 mostyfriedman

• The Algorithmi

Reputation: 728
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 11:36 AM

you will need to store your numbers in an array and sort them using a sorting algorithm, for you i would recommend the selection sort since it is the easiest sorting algorithm and also because i have written a snippet for it , check it out

### #8 barsoum89

Reputation: 0
• Posts: 25
• Joined: 20-February 09

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 12:55 PM

mostyfriedman, on 21 Feb, 2009 - 10:36 AM, said:

you will need to store your numbers in an array and sort them using a sorting algorithm, for you i would recommend the selection sort since it is the easiest sorting algorithm and also because i have written a snippet for it , check it out

i checked out your snippet but i haven't really done that sort of programming with Java yet and i'm only supposed to do this problem with the IF function.

```if (w>x)

{
double temp;
temp = w;
w = x;
x = temp;
}

if (w>y)
{
double temp2;
temp2 = w;
w = y;
y = temp2;
}

if (w>z)

{
double temp3;
temp3 = w;
w = z;
z = temp3;
}

```

does that look right for sorting the numbers from least to greatest?

### #9 Delta62

Reputation: 10
• Posts: 55
• Joined: 19-February 09

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 02:09 PM

barsoum89, on 21 Feb, 2009 - 09:38 AM, said:

i'm not sure how to display it in a MessageDialog.

This part is actually pretty easy. To get a message dialogue to show up, do this:

First import the swing components like this:
```import javax.swing.*;
```

Then, add a line of code whenever you want the dialog box to show up. You will need to pass the box 2 things. You can leave the first one as null, and the second needs to be a string:
```JOptionPane.showDialog(null, yourString);
```

If you are familiar with arrays at all, try putting all of your inputs into an array. After that, you could set up a while loop such as this:
```boolean sorted = false;
while(!sorted){
//if statements to re-arrange your numbers go here

//And then this will terminate the while loop eventually
if(myArray[0] < myArray[1] && myArray[1] < myArray[2] && myArray[2] < myArray[3]){
sorted = true;
}
}
```

That may be a lot to comprehend from one post, so post back if any of that isn't very clear.

### #10 barsoum89

Reputation: 0
• Posts: 25
• Joined: 20-February 09

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 02:29 PM

Delta62, on 21 Feb, 2009 - 03:09 PM, said:

barsoum89, on 21 Feb, 2009 - 09:38 AM, said:

i'm not sure how to display it in a MessageDialog.

This part is actually pretty easy. To get a message dialogue to show up, do this:

First import the swing components like this:
```import javax.swing.*;
```

Then, add a line of code whenever you want the dialog box to show up. You will need to pass the box 2 things. You can leave the first one as null, and the second needs to be a string:
```JOptionPane.showDialog(null, yourString);
```

If you are familiar with arrays at all, try putting all of your inputs into an array. After that, you could set up a while loop such as this:
```boolean sorted = false;
while(!sorted){
//if statements to re-arrange your numbers go here

//And then this will terminate the while loop eventually
if(myArray[0] < myArray[1] && myArray[1] < myArray[2] && myArray[2] < myArray[3]){
sorted = true;
}
}
```

That may be a lot to comprehend from one post, so post back if any of that isn't very clear.

i know how to display it but i'm not sure what goes after the "null, " part. i havent done arrays at all. i'm very new to java and this is a first year course that i'm taking and my teacher hasnt really taught us anything. i have taught myself most of this stuff i'm just confused on how to order them then display them in a message box. what i've done so far is entered the numbers, parsed them, and displayed them in the order they were entered. i want to display them in numerical order from least to greatest and i have no idea how to do that using the IF statement.

on a side now i would like to thank everyone for their time and their help it is greatly appreciated.

### #11 Delta62

Reputation: 10
• Posts: 55
• Joined: 19-February 09

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 02:47 PM

An array is used to store a collection of values. These can be ints, Strings, or any number of other data types.

You can declare an array of ints like so:
```int[] myArray = new int[4];
```

This will create an array called myArray with 4 'spaces' in it, that can contain any int value.

From here, you want to put each of the numbers into your array. Since this particular array has 4 spaces in it, you may add ints like this:
```myArray[x] = someInt;
```

where x is between 0 and 3 (inclusive).

So let's say the numbers that the user has entered are stored in variables called a, b, c, and d. You would want to do something like this:
```myArray[0] = a;
myArray[1] = b;
myArray[2] = c;
myArray[3] = d;
```

Now that you have an array containing the right values, you can sort them with if statements as follows:
```if(myArray[1] < myArray[0]){
int temp = myArray[0];
myArray[0] = myArray[1];
myArray[1] = temp;
}
```

You will need to write code to sort the other positions in the array, too.
We can work on fixing the dialog box once the sorting is finished.

### #12 barsoum89

Reputation: 0
• Posts: 25
• Joined: 20-February 09

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 10:11 PM

Delta62, on 21 Feb, 2009 - 03:47 PM, said:

An array is used to store a collection of values. These can be ints, Strings, or any number of other data types.

You can declare an array of ints like so:
```int[] myArray = new int[4];
```

This will create an array called myArray with 4 'spaces' in it, that can contain any int value.

From here, you want to put each of the numbers into your array. Since this particular array has 4 spaces in it, you may add ints like this:
```myArray[x] = someInt;
```

where x is between 0 and 3 (inclusive).

So let's say the numbers that the user has entered are stored in variables called a, b, c, and d. You would want to do something like this:
```myArray[0] = a;
myArray[1] = b;
myArray[2] = c;
myArray[3] = d;
```

Now that you have an array containing the right values, you can sort them with if statements as follows:
```if(myArray[1] < myArray[0]){
int temp = myArray[0];
myArray[0] = myArray[1];
myArray[1] = temp;
}
```

You will need to write code to sort the other positions in the array, too.
We can work on fixing the dialog box once the sorting is finished.

you guys are honestly the greatest and i thank you so much once again. i'm getting this ONE gay error which i dont understand why i'm getting it. it says "variable someInt might not have been initialized" but i clearly stated it at the top

```public class FourNbrSort {

public static void main (String[] args)  {
double someInt;
String s = javax.swing.JOptionPane.showInputDialog("Enter Four Numbers: ");

int i = s.indexOf(" ");
int j = s.indexOf(",");
String an = s.substring(i+1,j);
double w = Double.parseDouble(an.trim());

int k = s.indexOf(",", j+1);
String bn = s.substring(j+1,k);
double x = Double.parseDouble(bn.trim());

int l = s.indexOf(",", k+1);
String cn = s.substring(k+1,l);
double y = Double.parseDouble(cn.trim());

int m = s.indexOf(",", l+1);
String dn = s.substring(l+1);
double z = Double.parseDouble(dn.trim());

double[] myArray = new double [4];

myArray[4] = someInt;
myArray[0] = w;
myArray[1] = x;
myArray[2] = y;
myArray[3] = z;

if(myArray[1] < myArray[0])
{
double temp = myArray[0];
myArray[0] = myArray[1];
myArray[1] = temp;
}

javax.swing.JOptionPane.showMessageDialog(null,"Input list : "+s);

}

}

```

once again you guys are amazing and i'm pressing the "this was helpful" thingy

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Sorting Numbers in Java

Posted 21 February 2009 - 10:23 PM

barsoum89, on 21 Feb, 2009 - 09:11 PM, said:

once again you guys are amazing and i'm pressing the "this was helpful" thingy

Good
so I hope we can close this topic... 12 replies to fix a simple sort problem... guys we are loosing our efficiency

### #14 barsoum89

Reputation: 0
• Posts: 25
• Joined: 20-February 09

## Re: Sorting Numbers in Java

Posted 22 February 2009 - 01:35 PM

alright guys this is my final rough draft of this stupid code

```public class FourNbrSort {

public static void main (String[] args)  {

String s = javax.swing.JOptionPane.showInputDialog("Enter Four Numbers: ");

int i = s.indexOf(" ");
int j = s.indexOf(",");
String an = s.substring(i+1,j);
double w = Double.parseDouble(an.trim());

int k = s.indexOf(",", j+1);
String bn = s.substring(j+1,k);
double x = Double.parseDouble(bn.trim());

int l = s.indexOf(",", k+1);
String cn = s.substring(k+1,l);
double y = Double.parseDouble(cn.trim());

int m = s.indexOf(",", l+1);
String dn = s.substring(l+1);
double z = Double.parseDouble(dn.trim());

double[] myArray = new double [4];

double someInt;

myArray[4] = someInt;
myArray[0] = w;
myArray[1] = x;
myArray[2] = y;
myArray[3] = z;

if(myArray[1] < myArray[0])
{
double temp = myArray[0];
myArray[0] = myArray[1];
myArray[1] = temp;
}
if(myArray[2] < myArray[0])
{
double temp1 = myArray[0];
myArray[0] = myArray[2];
myArray[2] = temp1;
}
if(myArray[3] < myArray[0])
{
double temp2 = myArray[0];
myArray[0] = myArray[3];
myArray[3] = temp2;
}

if(myArray[2] < myArray[1])
{
double temp4 = myArray[1];
myArray[1] = myArray[2];
myArray[2] = temp4;
}
if(myArray[3] < myArray[1])
{
double temp5 = myArray[1];
myArray[1] = myArray[3];
myArray[3] = temp5;
}

if(myArray[3] < myArray[2])
{
double temp6 = myArray[2];
myArray[2] = myArray[3];
myArray[3] = temp6;
}

javax.swing.JOptionPane.showMessageDialog(null,"Input list : "+s+"\n"+
"Ordered list : "+myArray);

}

}
```

i only get one error "variable someInt might not have been initialized
myArray[4] = someInt;"
but that doesnt make sense cause on the first line i state it. does the code look right?

also i just have one last question....if i want to display them in order how do i do that?

This post has been edited by barsoum89: 22 February 2009 - 02:56 PM

### #15 Delta62

Reputation: 10
• Posts: 55
• Joined: 19-February 09

## Re: Sorting Numbers in Java

Posted 23 February 2009 - 01:10 AM

The problem here is a simple one...

The code that you have declares an int someInt:
```double someInt;
```

At this point, there is no value associated with the variable someInt, though. Just a data type. So when you use this statement:
```myArray[4] = someInt;
```

There is nothing to assign to myArray[4].
Actually, this is fine because myArray only has 4 values (remember that in Java, numbers start at 0 when incrementing, so myArray[4] is actually a fifth element in the list).
That being said, you should be able to delete this line entirely:
```myArray[4] = someInt;
```

to display the numbers once the sorting algorithm is done, try using another showMessageDialog command, but pass the objects from the list to it:
```JOptionPane.showMessageDialog(null, myArray[0] + " " + myArray[1] + " " + myArray[2] + " " + myArray[3]);
```