1

Created a simple Java program to add 2 whole numbers (which is not complete yet). I want help specifically on my variable "sum". Command prompt dictates it may have not been initialized,

enter image description here

I defined sum within the "if statements". How else should I define or what am I doing wrong here? Any help is greatly appreciated!

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args){
        //Objective: Calculate whole numbers to add, subtract, divide and multiply
        Scanner kbd1 = new Scanner(System.in);
        Scanner kbd2 = new Scanner(System.in);

        int num1, num2, sum;

        System.out.println("Enter two whole numbers: ");
        num1 = kbd1.nextInt();
        num2 = kbd2.nextInt();

        System.out.println("Now what would you like to do with these numbers? (Please input add, subtract, multiply, or divide)");
        Scanner oper = new Scanner(System.in);
        String operation;
        operation = oper.nextLine();

        if (operation == "add" || operation == "Add")
        {
            sum = num1 + num2;
        }
else
        if (operation == "subtract" || operation == "Subtract")
        {
            if (num1 > num2) {
                sum = num1 - num2;
            } else {
                sum = num2 - num1;
            }
        }
else
        if (operation == "multiply" || operation == "Multiply")
        {
            sum = num1 * num2;
        }
else
        if (operation == "divide" || operation == "Divide")
        {
            sum = num1 / num2;
        }

        System.out.println("The answer is: " + "/n" + sum);
    }
}

UPDATE:

Based on Sotirios Delimanolis, getlost, and other answers:

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args){
        //Objective: Calculate whole numbers to add, subtract, divide and multiply
        Scanner kbd1 = new Scanner(System.in);
        Scanner kbd2 = new Scanner(System.in);

        int num1, num2, sum;


        System.out.println("Enter two whole numbers: ");
        num1 = kbd1.nextInt();
        num2 = kbd2.nextInt();

        System.out.println("Now what would you like to do with these numbers? (Please input add, subtract, multiply, or divide)");
        Scanner oper = new Scanner(System.in);
        String operation;
        operation = oper.nextLine();

        if (operation.equals("add"))
        {
            sum = num1 + num2;
        }
else
        if (operation.equals("subtract"))
        {
            if (num1 > num2) {
                sum = num1 - num2;
            } else {
                sum = num2 - num1;
            }
        }
else
        if (operation.equals("multiply"))
        {
            sum = num1 * num2;
        }
else
        if (operation.equals("divide"))
        {
            sum = num1 / num2;
        }
else
        {sum = 0;}


        System.out.println("The answer is: " + "/n" + sum);
    }
}

This seems to work now, I had to compare Strings rather than to make explicit absolute values from the user input. Thanks guys!

aonepathan
  • 1,845
  • 2
  • 13
  • 15
  • 7
    What if (and this is the case here because of [this](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)) none of your `if` conditions returned `true`? – Sotirios Delimanolis Oct 17 '14 at 01:46
  • So you are saying to alter my if-statements to be converted to .equals() format? (Such as: if (operation.equals("add") {...) or actually change the operator from "==" to "="? – aonepathan Oct 17 '14 at 01:55
  • @Aonepathan First you have to understand about local variable and global variable declaration. Then about equals() and == . Please go through some tutorials on Java especially on String – gjman2 Oct 17 '14 at 02:01
  • @SotiriosDelimanolis Many many thanks for that useful information, went along with revising 'operation' to operation.equals("add"), worrked out! Thanks! – aonepathan Oct 17 '14 at 02:07
  • @gjman2 Understood, I'll take that advice. I'm actually practicing and training in Java now, I figured to ask you guys. Learn a lot more that way, apologies if this question was noobish! ;) – aonepathan Oct 17 '14 at 02:15

2 Answers2

2
System.out.println("The answer is: " + "/n" + sum);

because you haven't initialized int sum; you can't grantee to sum to initialize inside condition

example

int y;
if(condition not true){
   y=10;
}
System.out.println(y);

so same case on your code.what happen if y isn't initialized ?

you can fix by initializing sum

int sum=0;

and one more thing don't use

operation == "add"

for compare Strings you should use

operation.equals("add")
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
1

As @SotirosDelimanolis suggests, there is a path of execution where sum does not get a value (namely, where none of the tests in any of the ifs are true).
One fix is to do as @getlost suggests; the other is to add a final else clause, which will do whatever is appropriate to sum if none of the ifs succeed.

ccjmne
  • 9,333
  • 3
  • 47
  • 62
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 2
    More precisely, there is **no** path of execution where `sum` gets assigned any value :) – ccjmne Oct 17 '14 at 01:52
  • @ccjmne: I'm pretty sure that's not what the compiler has deduced. Once the problems with string comparisons have been fixed, the compiler will *still* have the same complaint. – Scott Hunter Oct 17 '14 at 01:54
  • Yeah, you're very right. It's just that sometimes, fixing the root of the problem is more sensible than simply swallowing the warning/error. However, your answer is definitely technically correct :) – ccjmne Oct 17 '14 at 02:00