'JAVA How do I read an input from the user containing math operators and numbers in one expression?

I have an assignment that asks me to create a Basic calculator, by taking an input from the user in one expressions as in "4 - 1" or "5 + 3", and I need to check each number(1-9) and the operator (+,-,*,/) to see whether it is applicable. My question is how do I check whether the expression has a valid operator and spaces in between numbers and operator? (I'm a beginner in Java, and thank you in advance to whoever answers)



Solution 1:[1]

Though I am not entirely sure where you are stuck, this example expression may be your best friend:

    String exampleString = "Example String";
    char exampleChar = example.charAt(3);

This statement is true:

    exampleChar == 'm'

EDIT FOR ADDITIONS

To read the user input, you would need to read different types of characters. To read a number using the Scanner class, you would use .nextInt() or .nextDouble() to check for spaces and symbols, using .next() only reads till the next break. Please elaborate where you are getting stuck/where we can be more specific :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1