简体   繁体   中英

How to take multiple data types in single line on java?

I am new at coding and now I am learning Java. I tryed to write something like calculator. I wrote it with switch case but then I realized I must take all inputs in single line. For example in this code I took 3 inputs but in 3 different lines. But I must take 2 input and 1 char in single line. First first number second char and then third number. Can you help me?

 Public static void main(String[] args) {
    int opr1,opr2,answer;
    char opr;
    Scanner sc =new Scanner(System.in);
    System.out.println("Enter first number");
    opr1=sc.nextInt();
    System.out.println("Enter operation for");
    opr=sc.next().charAt(0);
    System.out.println("Enter second number");
    opr2=sc.nextInt();

    switch (opr){
        case '+':
            answer=opr1+opr2;
            System.out.println("The answer is: " +answer);
        break;
        case '-':
            answer=opr1-opr2;
            System.out.println("The answer is: " +answer);
        break;
        case '*':
            answer=opr1*opr2;
            System.out.println("The answer is: " +answer);
        break;
        case '/':
            if(opr2>0) {
                answer = opr1 / opr2;
                System.out.println("The answer is: " + answer);
            }
            else {
                System.out.println("You can't divide to zero");
            }
        break;
        default:
            System.out.println("Unknown command");
        break;
    }

You can try something like this:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter number, operation and number. For example: 2+2");
    String value = scanner.next();

    Character operation = null;
    StringBuilder a = new StringBuilder();
    StringBuilder b = new StringBuilder();

    for (int i = 0; i < value.length(); i++) {
        Character c = value.charAt(i);
        // If operation is null, the digits belongs to the first number.
        if (operation == null && Character.isDigit(c)) {
            a.append(c);
        }
        // If operation is not null, the digits belongs to the second number.
        else if (operation != null && Character.isDigit(c)) {
            b.append(c);
        }
        // It's not a digit, therefore it's the operation itself.
        else {
            operation = c;
        }
    }

    Integer aNumber = Integer.valueOf(a.toString());
    Integer bNumber = Integer.valueOf(b.toString());

    // Switch goes here...
}

Note: didn't validate input here.

Try following way

System.out.print("Enter a number then operator then another number : ");
String input = scanner.nextLine();    // get the entire line after the prompt 
String[] sum = input.split(" ");

Here numbers and operator separated by "space" . Now, you can call them by sum array .

int num1 = Integer.parseInt(sum[0]);
String operator = sum[1];   //They are already string value
int num2 = Integer.parseInt(sum[2]);

Then, you can do as you did than.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM