Alex Jimborean
Alex Jimborean

Reputation: 67

Add two numbers instead of combining

I am trying to create a program in which a user can enter 2 numbers and the program will take the 2 numbers and multiply them to get an answer. However, for this specific example, I am simply trying to take 2 numbers from a user and I want Java to add them. eg 1+1=2, instead of 1+1=11.

My code:

import javax.swing.JOptionPane;

public class MultiplicationTables {

    public static void main(String args[]) {
        //declare variables

        String num1;
        String num2;
        int ans=0;

        num1=JOptionPane.showInputDialog(null,"Enter a number");
        num2=JOptionPane.showInputDialog(null,"Enter another number");

        ans=Integer.parseInt(num1);
        ans=Integer.parseInt(num2);


        JOptionPane.showMessageDialog(null,"Your answer is " + (num1+num2));
    }
}

Upvotes: 3

Views: 2472

Answers (6)

Bentaye
Bentaye

Reputation: 9756

You are using num1 and num2 which are Strings instead of ans which should be your sum as an int. Also you don't add correctly the 2 values into ans.

public static void main(String args[]){
    String num1 = JOptionPane.showInputDialog(null,"Enter a number");
    String num2 = JOptionPane.showInputDialog(null,"Enter another number");

    int ans = Integer.parseInt(num1);
    ans += Integer.parseInt(num2);

    JOptionPane.showMessageDialog(null,"Your answer is " + ans);
}

Upvotes: 6

SindreKjr
SindreKjr

Reputation: 259

I'd suggest you change the last line of your method to:

JOptionPane.showMessageDialog(null,"Your answer is " + (Integer.parseInt(num1) + Integer.parseInt(num2)));

This way, you don't need the "ans" variable (which in your code doesn't really do anything) at all. Instead, you can simply parse the int values and add them on the same line as you're writing the message.

If you want to use ans, try this:

ans = Integer.parseInt(num1) + Integer.parseInt(num2);

Then:

JOptionPane.showMessageDialog(null,"Your answer is " + ans);

Upvotes: 1

GhostCat
GhostCat

Reputation: 140457

Here:

num1=JOptionPane.showInputDialog(null,"Enter a number");
num2=JOptionPane.showInputDialog(null,"Enter another number");

The option pane returns strings, what you seem to understand, as you then go:

ans=Integer.parseInt(num1);
ans=Integer.parseInt(num2);

But then you use the + operator on your string results:

... +(num1+num2));

But + for strings concatenates them.

So instead of "adding" strings, you want to add the numbers, as in:

int1FromUser = Integer.parseInt(num1);
int2FromUser = Integer.parseInt(num2);

int sum = int1FromUser + int2FromUser;

That is all there is to this.

( and I took the freedom to use slightly better naming - keep in mind to use variable names that say something about the meaning of the thing they point to )

Upvotes: 6

Aniket Sahrawat
Aniket Sahrawat

Reputation: 12937

+ is used in addition when using Integer or int, and it also used to concatenate two Strings. In your case num1 and num2 are Strings, and hence it is concatenating the result. So you will have to change your code to reflect following changes:

ans = Integer.parseInt(num1);
ans += Integer.parseInt(num2); // add the result of parseInt to ans
//--^---------------------------
JOptionPane.showMessageDialog(null,"Your answer is " + ans ); // another one

Or alternatively:

JOptionPane.showMessageDialog(null,"Your answer is " + (Integer.parseInt(num1) + Integer.parseInt(num2)));

Upvotes: 3

davidxxx
davidxxx

Reputation: 131346

num1 and num2 are String.
So as you write num1+num2 you get the result of their concatenation.

In fact, you don't use the Integer.parseInt(); results.

Instead, do it to addition two int values:

int result = Integer.parseInt(num1) + Integer.parseInt(num2);

And display result :

JOptionPane.showMessageDialog(null,"Your answer is " + result);

Upvotes: 8

Suresh Atta
Suresh Atta

Reputation: 121998

You are doing it wrong. You are adding Strings in the end and ignoring the parsed Integers.

And also you using the same integer variable for both inputs.

So that should be

int ans1=0;
int ans2=0;

...

 ans1=Integer.parseInt(num1);
 ans2=Integer.parseInt(num2);

And in the end

JOptionPane.showMessageDialog(null,"Your answer is " +(ans1+ans2));

Upvotes: 3

Related Questions