Question: Need help on how to retrieve the first and second integers as well as the operator from a string input by the user! I'm stuck

Need help on how to retrieve the first and second integers as well as the operator from a string input by the user!

I'm stuck on step 9 of my assignment (goal is to have a calculator program) (see below)

9. Replace the output statement in case 1 with code that does the following: (a) Prompt the user to enter an expression of the form NUM OP NUM. (b) Read an int representing the first number, a String representing the operation, and an int representing the second number. (c) Use the charAt method of the String class to extract the character as position 0 in the string and store it in a variable of type char. (d) Use a switch statement with the char variable as the controlling expression to determine the operator the user entered. (e) If the operator is one of +, -, *, /, %, perform the operation on the numbers the user entered and display the result

(f) For the default case, display an error indicating the user entered an unknown operator.

I am very new to programming and have been able to grasp everything in my computer science course up until this most recent project. I had no problem up until step nine of the assignment. I cannot figure out how to retrieve the first and second integers as well as the operator from a string input by the user.

This is the code I have so far:

Scanner scan = new Scanner(System.in); int optionChosen; do { System.out.println("Choose one of the following options:"); System.out.println("1. Perform an arithmetic operation"); System.out.println("2. Apply a function"); System.out.println("3. Calculate a factorial"); System.out.println("4. Exit the program"); optionChosen = scan.nextInt(); switch (optionChosen) { case 1: System.out.println("Enter an expression of the form NUM OP NUM"); String expression = scan.next(); char firstNum = expression.charAt(0); //this and secondNum are supposed to be int, but I can't get char operation = expression.charAt(1); // the code to run when using int char secondNum = expression.charAt(2); System.out.println(expression); //temporarily using these to check the variables System.out.println(firstNum); System.out.println(operation); System.out.println(secondNum); switch (operation) { case '+': System.out.println("Result is: " + (firstNum + secondNum)); break; case '-': System.out.println("Result is: " + (firstNum - secondNum)); break; case '*': System.out.println("Result is: " + (firstNum * secondNum)); break; case '/': System.out.println("Result is: " + (firstNum / secondNum)); break; case '%': System.out.println("Result is: " + (firstNum % secondNum)); break; default: System.out.println("Unknown operator: " + operation); } break; case 2: case 3: case 4: System.out.println("Thank you for choosing Patt Moffitt's calculator!"); System.out.println(); System.exit(0); break; default: System.out.println("Invalid Choice"); } } while (optionChosen <= 3); } }

There is plenty more that I have to do in the assignment but I am having the hardest time troubleshooting this step. Any help or advice would be greatly appreciated.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!