Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

help with this prefix method

807607Oct 30 2006 — edited Oct 30 2006
I need help with this method. The program runs but i still hava problem for example i enter +56 and return 11 but when i tried to enter -+567 return 25 instead of 23 also when i tried to enter +45-3 it return 9 so after 5 the program stop it just return the addition of 4+5
public static int prefixEvaluation(String s){
  
  int result=0 ;
  int i=1;
  char ch = s.charAt(0);
  s=s.substring(i , s.length());// I want to be able to read all the character of the string every time i call the recursive method
  
  if (ch>='0'&& ch<='9'){
    result=((int)ch-48);
    System.out.println(result);
    return result;
  }
  else if (ch=='+'|| ch=='-'|| ch=='*'|| ch=='/'){
    char op=ch;
    System.out.println(ch);
      
    int operand1= prefixEvaluation(s);
    
    s = s.substring(i++);
    int operand2= prefixEvaluation(s);
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2006
Added on Oct 30 2006
1 comment
115 views