Results 1 to 4 of 4
  1. #1
    dacoolest is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Question Need help with a java program

    Hi, I have to convert this program (getValue method) to set it to use double inputs instead of int. I've been trying to make changes in it but not getting proper values as the string entered is being converted into the double value by the getValue method. I'm in a very urgent situation and please help me to get it written properly. Thanks.

    Java Code:
    import java.util.Stack;
    
    public class Expression {
        private String str;
        private Stack<Integer> st2 = new Stack<Integer>();
        private int j = 0;
        
        private void getValue() {
            int sign = -1;
            int nextNumber = 0;
            int prevNumber = 0;
            int value = 0;
            boolean numeric = true;
            
            if (str.charAt(j) == '+') 
            {
                sign = 1;
            }
            else if (str.charAt(j) == '-')
            {
                sign = -1;
            }
            
            j++;
            
            while (numeric == true)
            {
                value += Character.getNumericValue(str.charAt(j));
                if (j != (str.length()-1) )
                {
                    nextNumber = Character.getNumericValue(str.charAt(j+1));
                    if (nextNumber >= 0 && nextNumber <=9)
                    {
                        value = value*10;
                        j++;
                    }
                    else
                    {
                        numeric = false;
     
                    }
                }
                else
                {
                    numeric = false;
                }
                
            }
            if (sign == -1)
            {
                value = value*-1;
            }
            else if (sign == 1)
            {
                value = value * +1;
            }
            st2.push(value);
        }
        
        private String formatString(String str)
        {
                    String temp = "";
            
            Stack<Character> st3 = new Stack<Character> ();
            char ch = ' ';
            int tempNumber = 0;
            
            ch = str.charAt(0);
            if (ch != '+' && ch != '-') 
            {
                st3.push('+');
            }
            
            for (int i = 0; i < str.length(); i++)
            {
                ch = str.charAt(i);
                st3.push(ch);
                if (ch == '*' || ch == '/')
                {
                    tempNumber = Character.getNumericValue(str.charAt(i+1));
                    if (tempNumber >= 0 && tempNumber<=9)
                    {
                        st3.push('+');             
                    }
                    
                }
            }
            while(st3.isEmpty() == false)
            {
                temp += st3.pop();
            }
            
            str = new StringBuffer(temp).reverse().toString();
            return str;
        }
        
        private void multiply()
        {
            int left = 0;
            int right = 0;
            int value = 0;
            j++;
            getValue();
            right = st2.pop();
            left = st2.pop();
            Multiply m = new Multiply(left,right);
            value = m.evaluate();
            st2.push(value);
        }
        
        private void divide()
        {
            int left = 0;
            int right = 0;
            int value = 0;
            j++;
            getValue();
            right = st2.pop();
            left = st2.pop();
            Divide d = new Divide(left,right);
            value = d.evaluate();
            st2.push(value);
        }
        
    
    
        public void handle(String st) 
        {
            int result = 0;
            this.str = formatString(st);
            
            char current = ' ';
            
            for (j = 0; j < str.length(); j++)
            {
               current = str.charAt(j);
               if (current == '+' || current == '-')
               {
                   getValue();
               }
               if (current == '*')
               {
                   multiply();
               }
               if (current == '/')
               {
                   divide();
               }
            }
            
            
            while(st2.isEmpty() == false)
            {
                result += st2.pop();   
            }
            System.out.println(result);
        }   
        
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help with a java program

    Can you show us what you have tried so far. Then we can comment easily.

  3. #3
    dacoolest is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Need help with a java program

    Hi, thanks for your reply. I managed to get it working. I'm sorry that I could not mention it here. btw, its nice to see that u too from SL.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help with a java program

    If you feel that the solution is something interesting, then post it here. Your solution may helpful to others.

    BTW, nice to see you too, from SL.

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 05:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 06:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •