Thread: Sorting 4 Integers using only If statements

1. Member
Join Date
Feb 2011
Posts
6
Rep Power
0

Sorting 4 Integers using only If statements

This is my question

"Have the user enter 4 positive integers. Display these integers in sequential order from the smallest to the largest. For example, if the user enters 4, 14, 2, 7, then you should display 2, 4, 7, 14. Strive to make your programming code as simple as possible."

This is my code so far...

Java Code:
```public class ME38 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String number1 = "";
String number2 = "";
String number3 = "";
String number4 = "";

number1 = JOptionPane.showInputDialog("Please enter a positive value.");
number2 = JOptionPane.showInputDialog("Please enter a positive value.");
number3 = JOptionPane.showInputDialog("Please enter a positive value.");
number4 = JOptionPane.showInputDialog("Please enter a positive value.");

int intnum1 = 0;
int intnum2 = 0;
int intnum3 = 0;
int intnum4 = 0;

intnum1 = Integer.parseInt(number1);
intnum2 = Integer.parseInt(number2);
intnum3 = Integer.parseInt(number3);
intnum4 = Integer.parseInt(number4);

int [] numbers = {intnum1,intnum2,intnum3,intnum4};
int i;
boolean flag = true;
int temp;

while (flag){
flag = false;
for(i=0; i < numbers.length-1; i++ )
{
if(numbers[i]>numbers[i+1])
{
temp = numbers[i];
numbers[i]=numbers[i+1];
numbers[i+1]=temp;
flag=true;
}
}
}

System.out.println(numbers);
}

}```
I don't really know what to do from here... Any suggestions or help would be greatly appreciated.

EDIT: Updated to a more recent code using arrays and bubble sorts. I am stuck though, not quite sure how to get it to print or if my code is correct(
Last edited by Leirsgrios; 02-02-2011 at 05:36 AM.

2. If those are the restrictions then you are very limited. Try a massively nested if statement. Perhaps you can use Math.max. Another possibly is varags and recursion but you probably haven't covered those topics yet.

3. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
Hmmm...I may have been mistaken. A classmate said we can't use any of those, but I think I can use bubble sorts but I still have no clue what to do. My teacher has been sick and has given no instruction on any of this but he sent an email saying it was still due.

4. BubbleSort or any other sorting algorithm is used to sort a List or array of items. so saying that you can use BubbleSort but not an array makes no sense.

5. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
Heh, then I better learn how to use arrays as well! :P

I sincerely apologize for the confusion, I have no idea what I am doing.
Last edited by Leirsgrios; 02-02-2011 at 04:54 AM.

6. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
I updated my code! :) Need a little help on my arrays and bubble sort. I am not sure if i'm doing it right.

7. I can see on line 23 you have a problem. Wait, no I can't. Perhaps if you posted updated code and ask a specific question....

8. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
My question was in my edit section.

I am trying to figure out how to make it show the numbers in order in the console as well as wondering if my code for the bubble sort was correct.

When I run the program, in the console it gives me this

[I@1fdc96c

It's not exactly what I was hoping to get. :(

9. Go to the Object class in the Java API and read about the toString method. It explains what you are seeing.

More to the point you are trying to print an entire array and not the values inside the array. Do you expect the JVM to read your mind as to how you want the values displayed.

10. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
Ahhh! Thanks a ton Junky! Sorry for not being to clear on what I needed. I learned how to make arrays and bubble sorts between the time I started this thread and now. Haha, thanks again for a point in the right direction! :)

System.out.println(Arrays.toString(numbers)); seems to do the magic!

11. Or if you want to display the values in some other format, simply use a loop to iterate over the array.

Posting Permissions

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