1

Before marking this all duplicate please look at the code as it is done in a different way than in other questions and I would appreciate a fix relating to this code. It is pretty much a calculator that takes two numbers and an operator then prints the final number (and, if applicable, a remainder). I get the errors:

The local variable num3 may not have been initialized

The local variable rem may not have been initialized

Here is the code:

import java.util.Scanner;

public class JCalc {

public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    int num1;
    int num2;
    int num3;
    int rem;

    System.out.println("Welcome to JCalc!  The best calculator ever!");
    System.out.print("Please enter the first number: ");

    num1 = myScanner.nextInt();

    System.out.print("Please enter the second number: ");

    num2 = myScanner.nextInt();

    System.out.print("Please enter an operator (+, -, %, *): ");

    String op = myScanner.next();

    if (op == "+") {
        num3 = num1 + num2;
    }

    if (op == "-") {
        num3 = num1 - num2;
    }

    if (op == "%") {
        num3 = num1 - num2;
        rem = num1 % num2;
    }

    if (op == "*") {
        num3 = num1 * num2;
    }
    System.out.print("The answer is: ");
    System.out.print(num3);  //error

    if (op == "%") {
        System.out.print(" with a remainder of ");
        System.out.println(rem);  //error
    }






}

}

The last 2 brackets got a little messed up when I copy pasted them (sorry). Appreciate all the help I can get!

The Boss
  • 29
  • 1
  • 8
  • @MadProgrammer how is that a duplicate? He's not asking about comparing Strings – hotforfeature Sep 02 '13 at 07:02
  • 1
    @abmitchell Yes, I know saw the `String` comparison code and jumped on it, my bad. But once we solve the question he's just asked, they'll hit this problem as well :P – MadProgrammer Sep 02 '13 at 07:03

5 Answers5

1

The compiler isn't the best at following multiple if conditions. I would change:

int num3;
int rem;

to

int num3=0;
int rem=0;

Now they'll be unconditionally initialized. Basically, you can't have local variables unassigned, though class fields are OK.

Anyway, you should use equals( for strings. Use if("+".equals(op)) and such.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

In Java, all local variables must be declared before they can be used. The basic form of a variable declaration is shown here:

type identifier [ = value][, identifier [= value] ...] ;

The type is one of Java's datatypes. The identifier is the name of the variable. To declare more than one variable of the specified type, use a comma-separated list.

double pi = 3.14159; // declares an approximation of pi.
char x = 'x';        // the variable x has the value 'x'.  
  1. local variables
  2. local variables

Use the String.equals(String other) function to compare strings, not the == operator.
== compares the reference of the variable where .equals() compares the values which is what you want.

  1. string comparison
Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
0

Both variables should be initialized with a default value, this is a compiler warning.

num3 is never assigned outside of an if statement, so technically it could never be assigned (in the eyes of your compiler). This isn't good, so it throws that warning, same with rem.

It doesn't throw warnings for the other two since they are forcibly assigned by the blocking method Scanner.nextInt()

hotforfeature
  • 2,558
  • 1
  • 16
  • 24
0

String comparison is not done via the == mechanism. This compares the memory address of the objects, not their contents

Instead use "+".equals(op) instead

See How do I compare strings in Java? for more details

Java makes no gurentees to what values are assigned to local variables. This means, to be sure, you should always assign your own default values, for example.

int num1 = 0;
int num2 = 0;
int num3 = 0;
int rem = 0;
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0
  1. Initialize your variables

    int num3=0; int rem=0;

  2. Compare your string using equals() method.

Compare the variable 'op' using equals() method instead of '=='.

if (op.equals("+")) {}

Or if you want to use '==' itself to compare, then take the help of intern() method.

if (op.intern() == "+") {}