Inputting and sorting three integers The problem to the question is:


  Write a program that takes 3 integers as an input from the user using input dialog messages and sorts the three numbers.


I just want to know: is there any major differences between my code and the book's code in terms of efficiency or anything specific that I could have done better in my code? I am slowly learning Java and am trying to pick up the best techniques as I go along trying to learn this language. Also, I really don't understand the logic behind the book's answer. It seems as if they are only comparing two numbers, but how can they guess what the third number will be in the sort?

Note: I am following my textbook and the newest thing we have learned are if and else statements along with switch statements. I have not learned any looping and I am still in the basics. I am following Introduction To Java Programming 9th edition Daniel Liang and this is from the Chapter 3 exercises.

My solution:

package Chapter3Exercises;
import javax.swing.JOptionPane;

public class SortThreeIntegers 
{
    public static void main(String[] args) 
    {
        //Prompt user to enter three integers

        String stringNum1 = JOptionPane.showInputDialog(null, "Please enter 1st Integer: ");
        String stringNum2 = JOptionPane.showInputDialog(null, "Please enter 2nd Integer: ");
        String stringNum3 = JOptionPane.showInputDialog(null, "Please enter 3rd Integer: ");

        int num1 = Integer.parseInt(stringNum1);
        int num2 = Integer.parseInt(stringNum2);
        int num3 = Integer.parseInt(stringNum3);

        if ((num1 &gt; num2 &amp;&amp; num1 &gt; num3))
        {
            if(num2 &gt; num3)
            {
                System.out.print(num3 + " " + num2 + " " + num1);
            }
            else
                System.out.print(num2 + " " + num3 + " " + num1);
        }
        else if ((num2 &gt; num1 &amp;&amp; num2 &gt; num3))
        {
            if(num1 &gt; num3)
            {
                System.out.print(num3 + " " + num1 + " " + num1);
            }
            else
                {
                System.out.print(num1 + " " + num3 + " " + num2);
                }
        }
        else if ((num3 &gt; num1 &amp;&amp; num3 &gt; num2))
        {
            if(num1 &gt; num2)
            {
                System.out.print(num2 + " " + num1 + " " + num3);
            }
            else
                System.out.print(num1 + " " + num2 + " " + num3);
        }
        else
        {
            System.out.println("ERROR!");
        }

    }

}


Book's solution:

public class Exercise03_08 {
  public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);

    // Enter three numbers
    System.out.print("Enter three integers: ");
    int number1 = input.nextInt();
    int number2 = input.nextInt();
    int number3 = input.nextInt();

    if (number1 &gt; number2) {
      int temp = number1;
      number1 = number2;
      number2 = temp;
    }

    if (number2 &gt; number3) {
      int temp = number2;
      number2 = number3;
      number3 = temp;
    }

    if (number1 &gt; number2) {
      int temp = number1;
      number1 = number2;
      number2 = temp;
    }

    System.out.println("The sorted numbers are "
      + number1 + " " + number2 + " " + number3);
  }
}
The books solution, is definitely cleaner, and the biggest difference I can see is the books solution is actually sorting the variables, rather than just changing the display order.

In terms of efficiency, displaying stuff to the console is more efficient than popping up message boxes. There's less conditional logic in the books solution and a lot less code duplication. Other than that, there is very little difference in the sorting operation, the books maybe slightly more efficient due to less conditional checks.

Overall though, in my opinion the book provides the better solution, because it has virtually no duplicated code, conditionals are a single level deep (much easier to read), actually sorts the numbers in memory (if this program were to do more, your variables would already be sorted for you next time you need them, as opposed to having to duplicate the conditional logic, and overall it's much easier to understand what it's doing and what's going on.