1

Say A user inputs the following 100 + 12 - 1 / 3 Using Scanner, I get that input and then split it to 100,+,12,-,1,/3 Now what I want to do is calculate the total in the order provided. 112 then 111 then 37 so the answer is 37.

This is my current thought process but now I'm stuck on how to implement it in Java for the rest of the length and operators.

This only work for "1 + 1" or "1 / 1" But what I need is for some variable to hold the first 2 integers result and then -,*,/ or add based on the rest of the expression.

    Scanner numstr = new Scanner(System.in);
    String input = numstr.nextLine();
    String [] str = input.split(" ");
    int sum =0;
    int add =0;
    int temp;
    for (int i=0;i<str.length;i++) {
        if (i%2 >0 && str[i].equals("+"))
        {
sum = Integer.parseInt(str[i-1]) + Integer.parseInt(str[i+1]);
            //sum = add;
            System.out.println(sum);

        }
        temp =sum;

        else if (i%2 >0 && str[i].equals("-"))
        {
sum = Integer.parseInt(str[i-1]) - Integer.parseInt(str[i+1]);
            //sum = temp - Integer.parseInt(str[i+1]);
            System.out.println(sum);

        }
        //temp = sum;
        else if (i%2 >0 && str[i].equals("/"))
        {
sum = Integer.parseInt(str[i-1]) / Integer.parseInt(str[i+1]);
            System.out.println(sum);

        }
        else if (i%2 >0 && str[i].equals("*"))
        {
 sum = Integer.parseInt(str[i-1]) * Integer.parseInt(str[i+1]);
            System.out.println(sum);

        }
   else if (i%2 >0 && !str[i].equals("+")&&!str[i].equals("-")&&!str[i].equals("/")&&!str[i].equals("*"))
        {

            System.out.println("unknow operator");

        }

    }

so for 1 + 2 + 5 * 30 would be: 3, 8, 240

Cœur
  • 37,241
  • 25
  • 195
  • 267
Anika
  • 103
  • 1
  • 8
  • 2
    Does this answer your question? [Evaluating mathematical expressions](https://stackoverflow.com/questions/1151127/evaluating-mathematical-expressions). Also do a search on `Postfix Notation` and see if that gives you some ideas. – WJS Nov 02 '19 at 21:41
  • If there cannot appear any parenthesis then you only need your sum variable. You read the first number into it, and then alternatingly read operator and number and add it to your result until your input is exhausted. You can use the `nextInt()` and `next()` functions of the `Scanner` to read your numbers and operators. – Jannik Nov 02 '19 at 21:51
  • @WJS Hello, thank you but I have already exhausted all of the other similar topics discussed on the internet, I still can't seem to understand, how to go about it.... – Anika Nov 02 '19 at 22:01
  • @Jannik, how can I use nextInt, when the user will be providing 1 + 1 + 1 on one line? – Anika Nov 02 '19 at 22:04
  • The scanner class splits your input according to some rules depending on the method you call. For example if you call `nextLine` it splits at newline characters. If you use `next` it splits at spaces. and for `nextInt` it tries to read and parse the next characters as an integer value. – Jannik Nov 02 '19 at 22:47

0 Answers0