My code isn't working and I don't know why, I am using Eclipse Mars.

Here is my code.

import java.util.Scanner;
public class Calculator {
public static void main (String []args){
Scanner playerInput = new Scanner(System.in);
int x = playerInput.nextInt();
String type = playerInput.nextLine();
int y = playerInput.nextInt();
while(true){
switch(type){
case "+":
System.out.println(x + y);
System.exit(0);
case "-":
System.out.println(x - y);
System.exit(0);
case "x":
System.out.println(x * y);
System.exit(0);
case "/":
System.out.println(x / y);
System.exit(0);
default:
System.out.println("That is not a valid response, please choose everything again.");
continue;
}
}
}
}

This is the problem i get.

1 // What I typed in.

  • // What I typed in.
    Exception in thread "main" java.util.InputMismatchException // What I didn't type in.
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Calculator.main(Calculator.java:7)

That seems to be a problem with Java code. This is a Forum for Arduino C/C++ code.

The error seems to have been detected in line 7 of your code but it may be in an earlier line. Line 7 looks very like line 5 which does not seem to have caused a problem.

Try commenting out lines to see if you can pinpoint where the problem is.

Otherwise seek advice in a Java forum.

...R

what does the Scanner nextInt() function do? Read the documentation before answering this.

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextInt--

PaulMurrayCbr:
what does the Scanner nextInt() function do? Read the documentation before answering this.

I admire either your patience or your background knowledge.

The nice thing about Java (there are not many) compared to the Arduino is fairly decent documentation.

...R