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));
}
}
}
}
}
```
CodePudding user response:
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));
}
...
}
CodePudding user response:
remove the exceptions and screen shot the console results so I can check what line number the "Incompatible operand types Scanner and String" along with the codes for calculator class/object and all of its methods because I suspect that one of the arguments passed on it is incompatible with the parameters. Also work on proper naming of variables, kindly research camelCase, PascalCase, snake_cases for a more cleaner way of writing codes and also work on your indentations