0

I'm a beginner in java. And I don't have ability of complex coding in java. I want to make a program to solve mathematical expression given in string form which must contain only numbers and four basic arithmetic operators. I have written the below code with the little basics that I know. But when i run the code, it shows error. It says i can't parse object into double and so on...

I have tried to give precedence for four basic operators according to BODMAS rule. But i don't know how to give precedence for brackets if the user encloses the expression within brackets.

the below code might seem funny for experts those who can make it with just a few lines, but I am really a beginner.

Here is the code that i made: (comments are not written as this code is simple and anyone can understand)

import java.util.*;
import java.util.concurrent.TimeUnit;
class Calculator
{
public static void main(String[] args)
throws Exception
{
    int aaa =0;
    while(aaa==0){
        Scanner sc = new Scanner(System.in);
        System.out.println("\nEnter expression to solve : ");
        String expression = sc.nextLine();
        if(!expression.isEmpty()){
            int charlength = expression.length();
            String charat0 = ""+expression.charAt(0);
            String charatlast = ""+expression.charAt(charlength-1);
            ArrayList forops = new ArrayList();
            ArrayList forchars = new ArrayList();
            if(expression.contains(" ")){
                pWD("\nSpaces are not allowed",TimeUnit.MILLISECONDS,30);
            }else if(!expression.matches("[-+/*0-9]+")){
                pWD("\nEnter only numbers and operators",TimeUnit.MILLISECONDS,30);
            }else if(!charat0.matches("[0-9]+")||!charatlast.matches("[0-9]+")){
                pWD("first and last characters must be\nnumbers",TimeUnit.MILLISECONDS,30);
            }else if(expression.contains("//")||expression.contains("**")||expression.contains("--")||expression.contains("++")||
                     expression.contains("/+")||expression.contains("*/")||expression.contains("-*")||expression.contains("+-")||
                     expression.contains("/-")||expression.contains("*+")||expression.contains("-/")||expression.contains("+*")||
                     expression.contains("/*")||expression.contains("*-")||expression.contains("-+")||expression.contains("+/")){
                pWD("There must be SINGLE operator between\ntwo adjacent numbers",TimeUnit.MILLISECONDS,30);
            }else{
                String[] chararr = expression.split("(?<=[-+*/])|(?=[-+*/])");
                int chararrl =chararr.length;
                for(int nn=0;nn<chararrl;nn++){
                    forchars.add(chararr[nn]);
                }for(int mn=0;mn<chararrl;mn+=2){
                    String willparse = ""+forchars.get(mn);
                    double parseint= Double.parseDouble(willparse);
                    forchars.set(mn,parseint);
                }while(forchars.contains("/")){
                    int divindex = forchars.indexOf("/");
                    double prediv = forchars.get(divindex-1);
                    double nexdiv = forchars.get(divindex+1);
                    forchars.set(divindex,(prediv/nexdiv));
                    forchars.remove(forchars.indexOf(prediv));
                    forchars.remove(forchars.indexOf(nexdiv));
                }while(forchars.contains("*")){
                    int mulindex = forchars.indexOf("*");
                    double premul = forchars.get(mulindex-1);
                    double nexmul = forchars.get(mulindex+1);
                    forchars.set(mulindex,(premul*nexmul));
                    forchars.remove(forchars.indexOf(premul));
                    forchars.remove(forchars.indexOf(nexmul));
                }while(forchars.contains("-")){
                    int subindex = forchars.indexOf("-");
                    double presub = forchars.get(subindex-1);
                    double nexsub = forchars.get(subindex+1);
                    forchars.set(subindex,(presub-nexsub));
                    forchars.remove(forchars.indexOf(presub));
                    forchars.remove(forchars.indexOf(nexsub));
                }while(forchars.contains("+")){
                    int addindex = forchars.indexOf("+");
                    double preadd = forchars.get(addindex-1);
                    double nexadd = forchars.get(addindex+1);
                    forchars.set(addindex,(preadd+nexadd));
                    forchars.remove(forchars.indexOf(preadd));
                    forchars.remove(forchars.indexOf(nexadd));}
                double answer1 = forchars.get(0);
                String answer = Double.toString(answer1);
                pWD("\nFinal answer is : "+answer,TimeUnit.MILLISECONDS,80);
            }}else{
            pWD("Please enter something",TimeUnit.MILLISECONDS,30);
        }}
}public static void pWD(String data, TimeUnit unit, long delay)
throws InterruptedException {
    for (char ch:data.toCharArray()) {
        System.out.print(ch);
        unit.sleep(delay);
        }
    }
}

Please help me how to fix the error of parsing into double. And explain me in a little detail on how to solve expression if user encloses the expression in brackets.

Thank You.

Abhilash
  • 3
  • 3
  • What line does the error occur on? Even better, can you make an example with less code, isolating the point where the error occurs? – Phil Freihofner Feb 24 '17 at 08:27
  • You should print out the results of your `split` operation. Probably one of the substrings cannot be converted to a number. – Frank Puffer Feb 24 '17 at 08:28

1 Answers1

0

Since you are declaring a double variable, you cannot assign an int value to it. So I assume the best option you should have is to:

cast your int to double

double prediv = (double) forchars.get(divindex - 1);

It is just the type mismatch which you should take into account.

Dani_NJ
  • 48
  • 1
  • 7
  • You might want to refer to [this](http://stackoverflow.com/questions/5289393/casting-variables-in-java) link to get to know more on casting. – Dani_NJ Feb 24 '17 at 08:33
  • yes it worked. but when i run this in command prompt, it shows like this : Note: Calculator.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. I don't have idea what to do. help me... – Abhilash Feb 24 '17 at 08:45
  • What I can see is that you are using an ArrayList without specifying the type. If you wanna get rid of it try to manipulate your code according to this [link](http://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning). Your code was working in `Eclipse` IDE though! – Dani_NJ Feb 25 '17 at 04:33