Results 1 to 11 of 11
- 02-02-2011, 03:28 AM #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...
I don't really know what to do from here... Any suggestions or help would be greatly appreciated.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); } }
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 04:36 AM.
- 02-02-2011, 03:33 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.
- 02-02-2011, 03:40 AM #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.
- 02-02-2011, 03:48 AM #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.
- 02-02-2011, 03:51 AM #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 03:54 AM.
- 02-02-2011, 04:21 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.
- 02-02-2011, 04:30 AM #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....
- 02-02-2011, 04:36 AM #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. :(
- 02-02-2011, 04:42 AM #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.
- 02-02-2011, 04:50 AM #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!
- 02-02-2011, 05:03 AM #11
Similar Threads
-
Sorting 3 Integers Using If Else
By MSteinman in forum New To JavaReplies: 12Last Post: 02-19-2010, 12:52 PM -
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01-28-2010, 10:29 AM -
Integers and Lists
By TGH in forum New To JavaReplies: 8Last Post: 01-27-2010, 09:49 AM -
Using if statements to determine integers.
By IYIaster in forum New To JavaReplies: 5Last Post: 07-31-2009, 06:27 PM -
Random Integers
By www.kwalski.com in forum Java AppletsReplies: 8Last Post: 12-09-2007, 05:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks