2

I am quite new learner of java se, and i am coming to some problems. The code below should be a calculator. It works fine But. To calculate the output the input of the scanner class should be entered like this:
1: enter 1st number - hit enter key on the keyboard
2: enter the case +-* or / then again hit the enter in keyboard
3: enter the second number and again hit the enter key
4: Finally comes the output

so it looks like this

Calculator

2

-

1

Calculation is 1:

The question is is there a way to put it in a single row


1. Enter the both numbers 2-1 - hit the enter on the keyboard
2. The output shows like Calculation: 1

public class Calculator {

    public static void main(String... args) {

        int num1;
        int num2;

        Scanner scan = new Scanner(System.in);
        System.out.println("Calculator");

        num1 = scan.nextInt();
        String str = scan.next();
        num2 = scan.nextInt();

        System.out.print(("Calculation is " + calculate(num1, str, num2)));
        scan.close();

    }

    private static int calculate(int num1, String str, int num2) {

        switch (str.charAt(0)) {
        case '+':
            return num1 + num2;
        case '-':
            return num1 - num2;
        case '*':
            return num1 * num2;
        case '/':
            return num1 / num2;

        }
        return calculate(num1, str, num2);

    }
}
Kevin Wallis
  • 3,585
  • 3
  • 22
  • 39
Nikolay Sabev
  • 31
  • 1
  • 4
  • Possible duplicate of [Java reading multiple ints from a single line](http://stackoverflow.com/questions/23506429/java-reading-multiple-ints-from-a-single-line) – Kevin Wallis Dec 10 '16 at 08:36
  • As I understand it is not the duplicate. He wants the user input to be displayed in a single row – goto Dec 10 '16 at 09:01
  • You already can do that, just delimit each part with a space, like `2 - 1`. – Tom Dec 10 '16 at 11:18

2 Answers2

0

If all you want is the user's input to be displayed in a single line, then you can easily achieve this without changing a single line of code in your current implementation. You just simply need to separate your input with whitespace.

So rather than 2-1, simply use 2 - 1

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods... Scanner Java Documentation

Hope this helps!

Naz
  • 61
  • 3
0

yes it is possible, the way to do that is to use a string to capture the whole operation then break it into different part.`public class application {

public static void main(String[] args) {



    // get the operation from the user
    Scanner scan = new  Scanner(System.in);

    System.out.print("Enter your operation : ");

    String operation = scan.nextLine();


    // booleans to evaluate to true if a certain sign is use
    boolean isAddition = operation.contains("+");
    boolean isSubstration = operation.contains("-");
    boolean isDivision = operation.contains("/");
    boolean isMultiplication = operation.contains("*");

    if(isAddition){       
        // get result from the function in charge of processing the operation 
       int result = processOperation(operation, '+');
       //output the result
       System.out.println(" = "+result);
    }
    if(isSubstration){     
       // get result from the function in charge of processing the operation
       int result = processOperation(operation, '-');
       //output the result
       System.out.println(" = "+result);
    }
    if(isDivision){ 
    // get result from the function in charge of processing the operation 
       int result = processOperation(operation, '/');
       //output the result
       System.out.println(" = "+result);
    }
    if(isMultiplication){   
       // get result from the function in charge of processing the operation
       int result = processOperation(operation, '*');
       //output the result
       System.out.println(" = "+result);
    }

}

private static int processOperation(String operation, char sign){

        //remove all the whitespace in the string
        operation = operation.replaceAll("\\s+", "");

        // get the position of the sign operator in the String
        int signIndex = operation.indexOf(sign);

        //get the first number from the string which start from index 0 up to signIndex 
        int num1 = Integer.parseInt(operation.substring(0, signIndex));

        //get the second number from the string which is start from signIndex 
        int num2 = Integer.parseInt(operation.substring(signIndex+1));


        //evaluate the sign in use in order to perfom the appropriate operation 
    if (sign == '+') {
        return num1 + num2;
    } else if (sign == '-') {
        return num1 - num2;
    } else if (sign == '*') {
        return num1 * num2;
    } else {
        return num1 / num2;
    }
}

}`