stack overflow - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -3 -


i'm new java , know there more posts 1 can't apply them code current limited understanding of java. using eclipse.exe want program work operators , kinds of brackets when type expression please me got wrong~(sorry english bad)
still have problem java.lang.stringindexoutofboundsexception

package hww3; import java.lang.character.subset; import java.util.scanner;  public class demo {      public static void main(string[] args) {         // todo auto-generated method stub         scanner s=new scanner(system.in);         system.out.print("입력: ");         calculator cal=new calculator();          string susik=s.nextline();          system.out.println(susik);         cal.process(susik);      }  } class calculator{      public void process(string susik) {         susik=susik.replaceall(" ", "");         double result=calculate(susik);         system.out.println("="+result);     }       private double calculate(string susik) {          system.out.println(susik);          double reval=0;          if(susik.indexof("[")!=-1         ||susik.indexof("{")!=-1         ||susik.indexof("(")!=-1){                if(susik.indexof("[")!=-1){                 reval=calculate(susik,"[","]");             }              if(susik.indexof("{")!=-1){                 reval=calculate(susik,"{","}");             }              if(susik.indexof("[")!=-1){                 reval=calculate(susik,"(",")");             }          }else{          if(susik.indexof("+")!=-1){                 reval=add(getfirstterm(susik,"+"),getsecondterm(susik,"+"));         }else if(susik.indexof("-")!=-1){             reval=add(getfirstterm(susik,"-"),getsecondterm(susik,"-"));         }else if(susik.indexof("*")!=-1){             reval=add(getfirstterm(susik,"*"),getsecondterm(susik,"*"));         }else if(susik.indexof("/")!=-1){             reval=add(getfirstterm(susik,"/"),getsecondterm(susik,"/"));         }else{             //연산자가 없을 경우             reval=double.parsedouble(susik);         }     }         return reval; }      private double calculate(string susik, string frontbracket, string endbracket) {          string frontsusik="";          string bracketsusik="";          string endsusik="";          if(susik.indexof(frontbracket)!=0){             frontsusik=susik.substring(0,susik.lastindexof(frontbracket));         }else if(susik.indexof(endbracket)!=(susik.length()-1)){             endsusik=susik.substring(susik.lastindexof(endsusik));         }else         bracketsusik=susik.substring(susik.indexof(frontbracket)+1,susik.lastindexof(endbracket));           return calculate(frontsusik+calculate(bracketsusik)+endsusik);     }      private double getsecondterm(string susik,string operator){         return calculate(susik.substring(susik.indexof(operator)+1));     }      private double getfirstterm(string susik,string operator){         return calculate(0,susik.substring(susik.indexof(operator)));     }     public double add(double a,double b){         return a+b;     }     public double subtract(double a,double b){         return a-b;     }     public double nultiply(double a,double b){         return a*b;     }     public double divide(double a,double b){         return a/b;     }  } 

you substring operation incorrect getfirstterm, passing start index should 0 index of operator.

try this.

    private double getfirstterm(string susik,string operator){         return calculate(susik.substring(0,susik.indexof(operator)));     } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -