java - How can i calculate a string mix of operators and operands -
it giving error when operator encountered in between.i know operator cannot into
 converted int or other format.i using operator calculation reading byte codes , passing         enum defined.but string having operators having prob in handling these.please          me on this. ----my inputs 1 + 2 ----expected output-- 1 +  2=3---
error in line ---- b = integer.parseint(st.nexttoken());
--error in during exceution----- enter series-- 1 + 2
no of tokens:3 yo 1 go 1 available  byte info:10 ....... exception in thread "main" java.lang.numberformatexception: input string: "+"     @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65)     @ java.lang.integer.parseint(integer.java:484)     @ java.lang.integer.parseint(integer.java:527)     @ abc.main(abc.java:42)     not able rectify it. below code  import java.io.*; import java.util.stringtokenizer;  public class abc{ public static void main(string[] args) throws exception { system.out.println("hello world"); system.out.println("enter series"); bufferedreader br=new bufferedreader(new inputstreamreader(system.in)); string s=br.readline(); int a=0; int b=0; system.out.println(s); while ((br.readline()) != null)  { stringtokenizer st=new stringtokenizer(s);  while (st.hasmoretokens()) { int i=0; i=st.counttokens(); system.out.println("no of tokens:"+i); string token = st.nexttoken(); system.out.println("yo"); system.out.println(token); system.out.println("go");   a=integer.parseint(token); system.out.println(a);   if (st.hasmoretokens()) // before consuming token, make sure           {         system.out.println("available");         byte b1=(byte)br.read();         system.out.println("byte info:"+b1);                          // there's 1 available                          if (st.hasmoretokens()){               system.out.println(".......");         b = integer.parseint(st.nexttoken());            system.out.println("///////");  system.out.println(a); system.out.println("reached"); system.out.println(b); } if (b1==43) { system.out.println("go"); int foo = integer.parseint(calculate(operator.addition, a, b)); } else if (b1==45) { int foo = integer.parseint(calculate(operator.subtraction, a, b)); } else if (b1==42) { int foo = integer.parseint(calculate(operator.multiply, a, b)); } else if (b1==47) { int foo = integer.parseint(calculate(operator.divide, a, b)); }  } } } }  public enum operator {     addition("+") {         public int apply(int  x1, int x2) {             return x1 + x2;         }     },     subtraction("-") {          public int apply(int x1, int x2) {             return x1 - x2;         }     },  multiply("*") {          public int apply(int x1, int x2) {             return x1 * x2;         }     },      divide("/") {          public int apply(int x1, int x2) {             return x1 / x2;         }     };   // you'd include other operators too... private final string text;      private operator(string text) {         this.text = text;     }      // yes, enums *can* have abstract methods. code compiles...     public abstract int apply(int x1, int x2);      public string tostring() {         return text;     }  }  public static string calculate(operator op, int x1, int x2) {     return string.valueof(op.apply(x1, x2)); } }      
couple of issues:
- you asking input in string s not processing it, hence rmeove s variable , wherever referenced.
 define
string line;variable modify , update while loop as:while ((line = br.readline()) != null)
- you dont need line 
byte b1=(byte)br.read();have line feed i.e. enter key pressed when entering line your while loop should be:
declare operand1, operand2, count int declare operator char while tokenizer has more tokens optional validate string token count 3 middle token operator. read token if count == 0 operand1 = int(token) else if count == 1 operator = char(token) else operand2 = int(token) done
Comments
Post a Comment