0

Possible Duplicate:
How do I compare strings in Java?

Why can't my program use my assigned string operator to calculate the two integers? For some reason its as if the program is not accepting the input from the user.

import java.io.*;

public class IntCalc {
    public static void main (String [] args) throws IOException {
        BufferedReader kb = new BufferedReader (new InputStreamReader (System.in));

        System.out.println ("This program performs an integer calculation of your choice.");

        System.out.println ("Enter an integer: ");
        int x = Integer.parseInt (kb.readLine());

        System.out.println ("Enter a second integer: ");
        int y = Integer.parseInt (kb.readLine());

        System.out.print ("Would you like to find the sum, difference, product, quotient, or remainder of your product?: ");
        String operator = kb.readLine();

        int finalNum;

        if (operator == "sum") {
            int finalNum = (x + y);
        } else if (operator == "difference") {
            int finalNum = (x - y);
        } else if (operator == "product") {
            int finalNum = (x * y);
        } else if (operator == "remainder") {
            int finalNum = (x % y);
        }

        System.out.print ("The " + operator + " of your two integers is " + finalNum ".");
    }
}
Community
  • 1
  • 1
  • 1
    You need to use `.equals()` not `==` to compare the strings. Also see [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – verdesmarald Sep 22 '12 at 15:50
  • `finalNum "."` How does that even compile? – Waleed Khan Sep 22 '12 at 15:50
  • Check your variable scopes. As finalNum in any of your if/else if statements differ from the one defined before the if conditional, and differs from the one printed after wards. – Mahmoud Aladdin Sep 22 '12 at 15:52
  • You could also use `java.util.Scanner` instead of `BufferedReader + parseInt`. That would make your code cleaner. – Crozin Sep 22 '12 at 15:54

3 Answers3

4

You need to remove the int declarations in your if statement here. Also when comparing strings use String.equals(). Make sure too to initialise finalNum or the compiler will complain.

int finalNum = 0;

if (operator.equals("sum"))
{
   finalNum = (x + y);
}
else if (operator.equals("difference"))
{
   finalNum = (x - y);
}   
else if (operator.equals("product"))
{
   finalNum = (x * y);
}
else if (operator.equals("remainder"))
{
   finalNum = (x % y);
}

System.out.print ("The " + operator + " of your two integers is " + finalNum + ".");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Hey I got a little farther in the program but am still left with one error. error: variable finalNum might not have been initialized. What could that possibly mean? – user1691096 Sep 23 '12 at 03:26
  • You need to to assign the local value `finalNum` similar to the way I have done it above. – Reimeus Sep 23 '12 at 09:33
2

Instead of using operator == "sum" use operator.equals("sum").

Sandeep Panda
  • 723
  • 6
  • 9
0

A couple of points:

  • When you write int finalNum within your if-statements what you're actually doing is creating a new variable and assigning it a value. However the scope of this variable exists within that particular if-block only. As such, you don't see your outer finalNum variable getting updated.

  • Consider using equalsIgnoreCase(String anotherString) to compare whether the user's input is a sum, difference, product or a remainder. This is because in your case, you wouldn't be bothered if the user enters sum or SUM or Sum, they would ideally mean the same.

Sujay
  • 6,753
  • 2
  • 30
  • 49