0
How do you make Java look for specific characters in user input
I was working on a Java calculator and happened to run into these errors as I was testing the input (I imported Java Scanner); ./Playground/Program.java:24: error: incompatible types: String cannot be converted to Scanner if (way = "+") { ^ ./Playground/Program.java:24: error: incompatible types: Scanner cannot be converted to boolean if (way = "+") { ^ ./Playground/Program.java:25: error: bad operand types for binary operator '+' System.out.println(num1 + num2); ^ first type: Scanner second type: Scanner 3 errors
2 Answers
+ 4
If you want to work with chars:
char input = scan.next().charAt(0);
if(input == '+'){
...
}
" " -> String
' ' -> char
+ 4
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
String input = scan.next();
if(input.equals("+"){
...
}
For comparison:
primitives like int or char use ==
objects like String use .equals()