Results 1 to 11 of 11
  1. #1
    Leirsgrios is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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. #3
    Leirsgrios is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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. #5
    Leirsgrios is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #6
    Leirsgrios is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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. #8
    Leirsgrios is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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. #10
    Leirsgrios is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

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

Similar Threads

  1. Sorting 3 Integers Using If Else
    By MSteinman in forum New To Java
    Replies: 12
    Last Post: 02-19-2010, 01:52 PM
  2. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 11:29 AM
  3. Integers and Lists
    By TGH in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 10:49 AM
  4. Using if statements to determine integers.
    By IYIaster in forum New To Java
    Replies: 5
    Last Post: 07-31-2009, 07:27 PM
  5. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 06:49 PM

Posting Permissions

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