0

I am designing a simple calculator using Java but I can't seem to make it work. I'm new at programming I tried everything on scanning every line.
The output I am trying to get is when I input 1 + 1 I need to get the output of 2 and I want to implement the MDAS rule which the multiplication and division has higher precedence over addition and subtraction.

Specific output is:
Enter: 2+1=
index= 0
2
index= 1
1
2

import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.Throwable.*;

public class Final{
    public static void main(String[]args){

        Scanner in = new Scanner (System.in);
        System.out.print("Enter: ");
        String text = in.nextLine();

        int pipe[] = new int [text.length()];
        int index = 0;

        for(int i =0; i < text.length(); i++) { 
            if (text.charAt(i) == (int) ' ' || text.charAt(i) == (int) '\t')
                ;   // strip out white spaces                           
            else if (Character.isDigit(text.charAt(i))) {
                int n = 0;

                do {
                    n = n * 10 + text.charAt(i) - (int) '0';
                    i++;
                } 
                while (Character.isDigit(text.charAt(i)));  
                pipe[index] = n;
                System.out.println("index= " + index);
                System.out.println(pipe[index]);
                index ++;      
            } else if (text.charAt(i) == '+') {
                System.out.println("index= " + index);
                System.out.println("+");
                pipe[index-2] = pipe[index-2] + pipe[index-1];
                System.out.println("r: " + pipe[index-2]);
                index = index - 1;

             } else if (text.charAt(i) == '-') {
                System.out.println("index= " + index);
                System.out.println("-");
                pipe[index-2] = pipe[index-2] - pipe[index-1];

                System.out.println("r: " + pipe[index-2]);
                index = index - 1;
             } else if (text.charAt(i) == '*') {
                System.out.println("index= " + index);  
                System.out.println("*");
                pipe[index-2] = pipe[index-2] * pipe[index-1];
                System.out.println("r: " + pipe[index-2]);
                index = index - 1;
             } else if (text.charAt(i) == '/') {
                System.out.println("index= " + index);  
                System.out.println("/");
                pipe[index-2] = pipe[index-2] / pipe[index-1];
                System.out.println("r: " + pipe[index-2]);
                    index = index - 1;
             }
        }
        System.out.print(pipe[0]);
    }
}
Dumbo
  • 1,630
  • 18
  • 33
Kolin
  • 9
  • 4
  • 1
    what is the output you currently get? – Stultuske Mar 21 '18 at 07:32
  • Hello there, welcome to StackOverflow. Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. In other words: Please include your current output, the problem that you are facing and what your debugging led you to. – Ben Mar 21 '18 at 07:33
  • So you are currently processing all your input from left to right. What you are currently doing is as follows. Input = `2+1`. Process `2`: `pipe[0]=2`. Process `+`. You try to do `pipe[-1]+pipe[0]`. Error. You are currently not calculating in the right way. In order to calculate `a + b` you first need to calculate `a` and then `b`. Imagine `a = c * d` so the whole term is now `c * d + b`. You first need to calculate `a` then you can add `b` to it. So you should split up your String at `+` and `-` signs, calculate left and right part and add those together. – Ben Mar 21 '18 at 07:42
  • In other words: What you are trying to do is much harder than you might think. Have a look here for potential solutions but expect them to be quite a bit more complicated in comparison to your approach: https://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – Ben Mar 21 '18 at 07:44

1 Answers1

0

If you want to respect operator precedence, you'll need either a stack if working bottom up, or a recursive function working top down.

Paul Janssens
  • 622
  • 3
  • 9