Simple Java program to add two integer numbers inputted by the user

Words
637
Reading
3 min
Listen
Play
9y

Hello Everyone, Season’s Greetings. Below is a simple Java program using Eclipse IDE to add two numbers. The numbers are going to be inputted by the user which means the user can try different numbers and is not limited by the numbers defined in the program. I will explain the codes line by line below.

snap1.png

snap6.png

Line 1: Import Statement. JOptionPane class is imported so we can have access to methods predefined in the class.

Line 3: Package Name. When you start a new package, you need a name to save it as.

Line 4: Main Method. Every Java program needs a main method else the program will not run.

Line 5: Variable Declaration. A String variable is declared to hold a value of String Type. String type variables are words; e.g “Eat”, “Go”, “Hello Steemit”.

Line 6: Variable Declaration. A String variable is declared to hold a value of String Type. String type variables are words; e.g “Eat”, “Go”, “Hello Steemit”.

Line 7: Variable Declaration. An Integer variable is declared to hold a value of Integer Type. Integer type variables are whole numbers; e.g 1, 5, 234.

Line 8: Variable Declaration. An Integer variable is declared to hold a value of Integer Type. Integer type variables are whole numbers; e.g 1, 5, 234.

Line 9: Variable Declaration. An Integer variable is declared to hold a value of Integer Type. Integer type variables are whole numbers; e.g 1, 5, 234.

Line 11: Assignment Statement. The variable firstNumber is assigned a value. The showInputDialog method is called from the JOptionPane class imported earlier. Variable will hold whatever value user inputs.

Line 12: Conditional Statement. A Conditional Statement in case the user decides to close the program.

Line 13: A Pop up appears on the screen with a message.

Line 14: A line of code to terminate the program if the user closes the program.

Line 15: Conditional Statement closing brace.

Line 16: Resulting else statement to set alternative if the ‘if’ condition isn’t met.

Line 17: Assignment Statement. The variable secondNumber is assigned a value. The showInputDialog method is called from the JOptionPane class imported earlier. Variable will hold whatever value user inputs.

Line 18: Conditional Statement. A Conditional Statement in case the user decides to close the program.

Line 19: A Pop up appears on the screen with a message.

Line 20: A line of code to terminate the program if the user closes the program.

Line 21: Conditional Statement closing brace.

Line 22: Resulting else statement to set alternative if the ‘if’ condition isn’t met.

Line 23: Conversion. The parseInt() method is called to convert a String variable to an Integer variable. The String variable is then stored in an Integer type variable.

Line 24: Conversion. The parseInt() method is called to convert a String variable to an Integer variable. The String variable is then stored in an Integer type variable.

Line 26: Arithmetic Operation. This adds up the two variables and assigns the result the variable ‘sum’.

Line 27: The showMessageDialog Method is called to display the result on the screen.

Line 28: Conditional Statement closing brace.

Line 29: Conditional Statement closing brace.

Line 30: Main Method closing brace

Line 31: Package closing brace.

snap2.png

When the program is run, the user inputs the first number.

snap3.png

User inputs second number.

snap4.png

The program sums up the two numbers

snap5.png

Program terminates if user clicks 'cancel' or the close button.

github proof.png

Proof of Github account

file.png

File in Github

code1.png

code2.png

Source codes form Github account

editorsnap.png

Snapshot from Eclipse IDE Editor.

This is the block of code

import javax.swing.JOptionPane;
public class willugoUtopianSteemit {
    public static void main (String args []) {
        String firstNumber;
        String secondNumber;
        int num1;
        int num2;
        int sum;
        firstNumber = JOptionPane.showInputDialog("Enter the First Number");
        if (firstNumber == null) {
            JOptionPane.showMessageDialog(null, "You have quit the program");
            System.exit(0);
        }
        else {
            secondNumber = JOptionPane.showInputDialog("Enter the Second Number");
            if (secondNumber == null) {
                JOptionPane.showMessageDialog(null, "You have quit the program");
                System.exit(0);
            }
            else {
                num1 = Integer.parseInt(firstNumber);
                num2 = Integer.parseInt(secondNumber);
                
                sum = num1 + num2;
                JOptionPane.showMessageDialog(null, "The sum of both numbers is " + sum);
            }
        }
    }
}

You can download Java Development Kit (JDK) and Eclipse IDE if you want to give this a try.

Java Development Kit(JDK) - http://www.oracle.com/technetwork/java/javase/downloads/index.html

Eclipse IDE - http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/oxygen2

This program can be a lot shorter. This is just for illustration purposes.

Thank you for reading.



Posted on Utopian.io - Rewarding Open Source Contributors

Simple Java program to add two integer numbers inputted by the user | Ecency