You can use String.split()
Scanner scanner = new Scanner(System.in);
String userInput = scanner.nextLine();
String[] plusSplit = userInput.split("+");
You will then have an array where each element is added. So for example "3+4+5" will give you a String array of length 3 where the 1st element is 3, the second 4, the third 5.
You can perform nested splits in order of operator strength.
So split on division first (remember to cast to double to avoid truncation). Then for each element in your division split, split on multiplication. Then addition, subtraction, etc. You can then traverse backwards using Integer.parseInt()/Double.parseDouble to reverse engineer your values until you get back to your first operation.
This is not a trivial but fun exercise that will give you great practice with the String
class, arrays
, java operators
, and using variables to hold your intermediate values.