sincity
sincity

Reputation: 38

how to ascend/descend integers in java

yes I did do research (including this site, MSDN, and some others) before I came here but none of it really helped me (just started coding) and I still don't get it. I am trying to make a java program that takes the 3 integer numbers you type in and puts them in from greatest to least. The trouble I am having is that whatever number is the highest it only displays that in the console and gets rid of the others. Can anybody shed any light or offer me any advice on what I am doing wrong? I would appreciate it.

Here is my code

import javax.swing.*;

public class sortingintegers {

    public static void main(String[] args) {


        // TODO Auto-generated method stub
                String variable1, variable5;
                int variable2, variable3, variable4;        

                variable1 = JOptionPane.showInputDialog(null, "Enter the first integer? ", 
                        "Exercise3_8", JOptionPane.QUESTION_MESSAGE);       
                variable2 = Integer.parseInt(variable1);    

                variable1 = JOptionPane.showInputDialog(null, "Enter the second integer? ", 
                        "Exercise3_8", JOptionPane.QUESTION_MESSAGE);       
                variable3 = Integer.parseInt(variable1);

                variable1 = JOptionPane.showInputDialog(null, "Enter the third integer? ", 
                        "Exercise3_8", JOptionPane.QUESTION_MESSAGE);
                variable4 = Integer.parseInt(variable1);


                //First Phase

                if (variable2> variable3 && variable2> variable4) {             
                    System.out.println(variable2);

                }

                else if (variable3> variable2 && variable3> variable4) {                
                    System.out.println(variable3);
                }

                else if (variable4> variable2 && variable4> variable3) {                
                    System.out.println(variable4);
                }


                //Second Phase

                if (variable2> variable3 && variable2> variable4) {             
                    System.out.println(variable2);
                }

                else if (variable3> variable2 && variable3> variable4) {                
                    System.out.println(variable3);
                }

                else if (variable4> variable2 && variable4> variable3) {                
                    System.out.println(variable4);
                }


                //Third Phase

                if (variable2> variable3 && variable2> variable4) {             
                    System.out.println(variable2);
                }


                else if (variable3> variable2 && variable3> variable4) {                
                    System.out.println(variable3);
                }

                else if (variable4> variable2 && variable4> variable3) {                
                    System.out.println(variable4);
                }

    }

}

Upvotes: 2

Views: 242

Answers (3)

sincity
sincity

Reputation: 38

Thanks to everybody that helped me, tried making an array but I still need to work on it. This is what I ended up doing and it worked.

import javax.swing.JOptionPane;

public class sortingintegers {

    private static char[] var1;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String integer1 = JOptionPane.showInputDialog("Enter the first integer:");
        String integer2 = JOptionPane.showInputDialog("Enter the second integer:");
        String integer3 = JOptionPane.showInputDialog("Enter the third integer:");

        int num1 = Integer.parseInt(integer1);
        int num2 = Integer.parseInt(integer2);
        int num3 = Integer.parseInt(integer3);

        if (num1 >= num2 && num1 >= num3 && num2 >= num3){          
            String var1 =(" The sorted numbers are "+ num3+ " "+ num2+ " " +num1);
            JOptionPane.showConfirmDialog(null,  var1);
            System.out.print(var1);
        }

        if (num1 >= num3 && num3 >= num2 && num1 >= num2){
            String var2 =(" The sorted numbers are "+ num2+ " "+ num3+ " " +num1);
            JOptionPane.showConfirmDialog(null,  var2);
            System.out.print(var2);
        }

        if (num2 >= num3 && num2 >= num1 && num3 >= num1){
            String var3 =(" The sorted numbers are "+ num1+ " "+ num3+ " " +num2);
            JOptionPane.showConfirmDialog(null,  var3);
            System.out.print(var3);
        }

        if (num2 >= num1 && num2>= num3 && num1 >= num3){
            String var4 =(" The sorted numbers are "+ num3+ " "+ num1+ " " +num2);
            JOptionPane.showConfirmDialog(null, var4);
            System.out.print(var4);
        }

        if (num3 >= num1 && num3 >= num2 && num1 >= num2){
            String var5 =(" The sorted numbers are "+ num2+ " "+ num1+ " " +num3);
            JOptionPane.showConfirmDialog(null, var5);
            System.out.println(var5);
        }

        if (num3 >= num2 && num3 >= num1 && num2 >= num1){
            String var6 =(" The sorted numbers are "+ num1+ " "+ num2+ " " +num3);
            JOptionPane.showMessageDialog(null, var6);
            System.out.print(var6);
        }   
    }
}

Upvotes: 0

Dandalf
Dandalf

Reputation: 2425

As a beginner, I'm sure you haven't learned about arrays or sorting algorithms yet. You are on the right track to finding the first number, but you need to use nested if statements to find the second and third numbers.

So let's take your first if statement:

if (variable2> variable3 && variable2> variable4) {             
    System.out.println(variable2);
    //Inside here, you know that variable 2 is first.  So is variable3 or variable4 second?
}

To figure out which variable is second, use an if statement comparing variable3 to variable4. Inside of that if statement, you know which variable is second, and then which variable is third since there is only one left.

After you finish the situation where variable2 is first, handle the situations where variable3 is first and variable4 is first in a similar fashion.

Upvotes: 0

Vitruvie
Vitruvie

Reputation: 2337

Simply store the numbers as an array or list (Whenever you want to call things var1, var2, var3, etc, ask yourself whether using an array is correct), and then sort them.

int[] nums = new int[3];
for (int i = 0; i < nums.length; i++) {
    nums[i] = JOptionPane.showInputDialog(null, "Enter integer " + i, 
                    "Exercise3_8", JOptionPane.QUESTION_MESSAGE);
}
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
    System.out.println(nums[i]);
}

Upvotes: 3

Related Questions