java - Inserting spaces In between operand and operators, getting String out of Bounds -


i have assignment create program converts infix expressions postfix. needed insert spaces in between operand , operators, keep getting stringindexoutofbounds reason. here's java code process.

public class processor { public string addspace(string str){     string finalstr = "";     (int = 0; < str.length(); i++) {         if(character.isdigit(str.charat(i))){             int x = i;             string temp = "";             do{                 temp+=str.charat(x);                 x++;             }while(character.isdigit(str.charat(x)));             finalstr+=(temp+" ");             system.out.println(temp+" added final");             i=(x-1);             system.out.println(x+" x , "+i);         }         else if(isoperator(str.charat(i))){             finalstr+=(str.charat(i)+" ");         }     }     return finalstr; }  public boolean isoperator(char a){     switch(a){         case '+':         case '-':         case '/':         case '*':         case '(':         case ')':         return true;         default: return false;     } } 

in loop

do {     temp += str.charat(x);     x++; } while (character.isdigit(str.charat(x))); 

you increase x , take character @ position x without checking if character exists. @ end of string if character digit go on length of string


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -