Comparing Two Integers


We are to Write an application that asks the user to enter two integers, obtains the numbers from the user and displays the larger number followed by its symbol in an information dialog. if the numbers are equal, print the message "these numbers are equal".

For us to use the information dialog, we will need to import the java package javax.swing.JOptionPane, which is used to provide some standard confirm and input dialog box. 

These dialog boxes will then be used to display information or get input from the user instead of displaying in a command prompt.
We will also be making use of our IF statement to compare these two numbers that the user will input to determine which one is larger or smaller.

In Java user input is normally in form of strings and not integers, so when the user inputs an integer as a string, we need to convert it to its corresponding integer by calling the Integer.parseInt() method.



import javax.swing.JOptionPane;

  public class Comparison{
 
  public static void main(String args[])
  {
    String firstNumber;
    String secondNumber; 
    String result;
 
    int number1;
    int number2; 

    firstNumber = JOptionPane.showInputDialog("Enter first integer:");

    secondNumber = JOptionPane.showInputDialog("Enter second integer:");

    number1 = Integer.parseInt(firstNumber);
    number2 = Integer.parseInt(secondNumber);

    result = "";

    if(number1==number2)
      result = result + number1 + "==" + number2;
    if(number1!=number2)
    result = result + number1 + "!=" + number2;
    if (number1 < number2)
      result = result + "\n"+ number1 + "<" + number2;
    if(number1 > number2)
      result = result +"\n" + number1 + ">" + number2;
    if(number1 <= number2)
      result = result + "\n" + number1 + "<=" + number2;
    if(number1 >=number2)
      result = result + "\n" + number1 + ">=" + number2;
 

    JOptionPane.showMessageDialog(null, result, "Comparison Results",JOptionPane.INFORMATION_MESSAGE);
 
    System.exit(0);
 
  }
}

Comments

Popular posts from this blog

Body Mass Index Calculator In C

Adding two Integers in Java Application

Calculating Product, sum, difference, and quotient of Five Integers