Stefan P Auckland Stefan P Auckland - 8 months ago 49
Java Question

building basic calculator (cant get user input for the operator)

so I'm brand new to programming, trying to build my 5th program, a basic calculator.

so far i can ask for users to input an integer correctly;

public static void main(String[] args) {
Scanner keyboard = new Scanner(;
int value1;
System.out.print("Enter 1st value: ");
value1 = keyboard.nextInt();
int value2;
System.out.print("Enter 2st value: ");
value2 = keyboard.nextInt();
char op;
System.out.print("Enter operator (/*-+): ");
op =;
int result = (value1 value2);

then I tried the same for the operator, and messing around with it i have this.

However this doesn't work and i get "erroneous sym type" as the error.
I have looked around and tried some other people code but it usually contains IF functions and lots of lines of code and I'm trying to keep it simple, unless that is the only way to do it?

Thanks in advance

Edit - full error message
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Scanner.nextChar
at calculator.Calculator.main(
Enter operator (/*-+): C:\Users\shott_000\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1


Surprisingly(?), Scanner does not have a nextChar method. Just read the operator into a String and you should be OK:

String operator;
System.out.print("Enter operator (/*-+): ");
operator =;

You should also probably validate that the inputted string is a valid operator. E.g.:

Set<String> validOpeators = new HashSet<>(Arrays.asList("/", "*", "-", "+"));
if (!validOperators.contains(operator)) {
    throw new IllegalArgumentException
              ("Operator must be one of " + validOperators);