-4
package thenewboston;
import java.util.Scanner;
public class apple {
    public static void main(String args[]) {
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter your first num");
        double fnum, snum, answer;
        String oper;
        fnum = myScan.nextDouble();
        System.out.println("Enter your second num");
        snum = myScan.nextDouble();
        System.out.println("Enter your operation");
        if (oper.equals("+")) {
        answer = fnum + snum;
        } else if (oper.equals("-")){
            answer = fnum - snum;
        } else if (oper.equals("*")) {
            answer = fnum * snum;
        } else if (oper.equals("/")){
            answer = fnum / snum;
        } else {
            System.out.println("Please choose a valid operation");
        }
        System.out.println("Your answer is: " + answer);
    }

}

Hi. Im trying to use .equals() in order to create a basic calculator but there is an error showing up on oper when I write oper.equals("...").

3 Answers3

0

You should assign value to oper.

String oper = "*";
tuğrul altun
  • 131
  • 1
  • 4
0

You are probably getting an error because you never actually set oper to anything before using it.

...
snum = myScan.nextDouble();
System.out.println("Enter your operation");
oper = myScan.nextLine(); //<-- You were missing this.
if (oper.equals("+")) {
answer = fnum + snum;
...
ggmathur
  • 833
  • 5
  • 12
0

You didn't ask the user to input an operation, therefore, your oper can't match any of the things you say later.