1

I keep getting the following errors:

  • Incompatible operand types Scanner and String
  • Incompatible operand types int and String before I added the int op = Integer.valueOf(operator) line it kept giving me errors when I would name my cases. I'm still very new it's the second code I've written for my class so please keep the explanation simple if at all possible (there is also a calculator class not shown here if there's any confusion about that) :
//change the package name to calculatorClass///////////////

import java.util.Scanner;

public class assignmentB {

    public static String String(int y,int x, String name, String str) throws Exception {
        if (y < 1)
            throw new Exception("Value must be larger than 0.");
        if (x < 1)
            throw new Exception("Value must be larger than 0");
        return name;
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Calculator c1 = new Calculator();
        Scanner input = new Scanner(System.in);
        c1.name = "The total is $";
    
try {
    }   catch (Exception e1){
        System.out.println(e1);
    }
    {
        
        int x, y, s, a, m;
        String name1;
        
        
        System.out.println("Red River College");
        System.out.println("Custom Calculator");
        
        System.out.println("Enter first value: ");
        x = input.nextInt();
        
        System.out.println("Enter second value: ");
        y = input.nextInt();
        
        System.out.println("Enter operation(a=Add, s=Subtract, m=multiply): ");
        String operator = input.nextLine();
        int op =Integer.valueOf(operator);
        
        switch (op) {
        case 1:
            if (input == "s") {
            System.out.println(c1.name + c1.subtract(x,y));
            }
        case 2:
            if (input == "a") {
            System.out.println(c1.name + c1.add(x,y));
            }
        case 3:
            if (input == "m") {
            System.out.println(c1.name + c1.multiply(x,y));
            }
        }
        
     
        
    }   
    }
}

    ```
1
  • 3
    Please provide the exact error message and indicate which line and column it occurs on. But you appear to have your op variable and your Scanner variable mixed up. And once you know the operator you don't need to do another test: the case takes you to the right place.
    – user207421
    Jun 24, 2022 at 4:10

1 Answer 1

5

You have defined input to be a Scanner, with this line:

Scanner input = new Scanner(System.in);

You can then use input to do scanner things, as you're doing here:

x = input.nextInt();

So far so good.

But later in the code, you do this (and a few variations that are similar):

if (input == "s") {
    ...
}

This isn't valid. The Java compiler is telling you that it will not allow you to use == to compare a Scanner (that is, input) against a String (which in this example is "s"). The compiler is giving you an error to say: input == "s" isn't allowed.

It isn't clear what your intent is (that is, how you want the code to actually behave), but the issue is definitely with those three if statements. Each is attempting to compare a java.util.Scanner with a java.lang.String (one is "s", one is "a", the last is "m"). So you'll need to fix those to do whatever it is you're trying to do.

As a guess, maybe you want to read a new String input from the scanner, and then compare that to "s", etc.? If so, that could look something like below. Note that to compare strings you should use equals() (don't use == to compare strings, or any other objects).

String newString = input.next();
switch (op) {
    case 1:
        if (newString.equals("s")) {
            System.out.println(c1.name + c1.subtract(x, y));
        }
    ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.