Aidan L. Aidan L. - 3 years ago 267
Java Question

I have 2 java errors that I need help! Errors: incomparable types: Scanner and String & bad operand types for binary operator '+'

I have two java errors that I need help on solving, please help!!

Error 1: incomparable types: Scanner and String

Error 2: bad operand types for binary operator '+'

Here is my code:

import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
x.nextInt();
Scanner y = new Scanner(System.in);
y.nextInt();
Scanner function = new Scanner(System.in);
function.next();
if (function == "add") {
int sum = x + y;
System.out.println(sum);
}

Answer Source

You can't use + for a non-primitive type (or String). In your case, you try to use it for a Scanner reference.

You probably meant:

Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
String function = scanner.next();
if (function == "age") {
    int sum = x + y;
    System.out.println(sum);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download