Space recognition in java

I'm a newbie and I started checking out some things, so I made a calculator that required only one line. It works when I do this in this format: Int space String space int. EG: 10 + 50. If I do not deal with spaces, for example, 50 + 50, the program fails. Is there a way to recognize spaces in Java? I say that I am noob, but I found that I did an excellent job with this task, which supported me. Here is the code:

package Tests;

import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter your values : ");
    int first = input.nextInt();
    String character = input.next();
    int second = input.nextInt();
    int answer = 0;


    switch (character) {
    case "*":
        answer = first * second;
        break;
    case "/":
        answer = first/second;
        break;
    case "-":
        answer = first-second;
        break;
    case "+":
        answer = first+second;
        break;
    default:
        System.out.println("Failed to recognise character");
        break;

    }

    System.out.println("Answer : " + answer);
}
}
+4
source share
3 answers

Scanner#next , Scanner . - , , , . .

Scanner input = new Scanner(System.in);
input.useDelimiter("\+|\-|\*|\\");

.

+6

, (.. nextInt()), , , nextLine() .

, , , , , , http://www.antlr.org/ .

+1

, Java , Scanner, ( ). Scanner, , , .

Java . input.nextLine() .

, , indexOf, substring, trim and Integer.parseInt()

0

All Articles