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.