Results 1 to 3 of 3
  1. #1
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Angry Program running indefinitely

    This code is free of errors, but for some reason it takes a lot of time to execute. It is supposed to process a mathematical expression in the form of a string and give a numerical answer but when I enter the expression it just keeps running. I left it to run for at least to minutes and even then it did not get me a result.... The string was 1*3.......

    Could someone tell me what the problem is please? Any help appreciate it.

    public class calculate
    {
    private String expression;
    private double first;
    public calculate(String s)
    {
    expression=s;
    first=number();

    }
    public double number()
    {
    String num ="";
    int i=1;
    String temp=expression.substring(i-1,i);
    while(temp.compareTo("1")==0 || temp.compareTo("2")==0 || temp.compareTo("3")==0 || temp.compareTo("4")==0 || temp.compareTo("5")==0 ||
    temp.compareTo("6")==0 || temp.compareTo("7")==0 || temp.compareTo("8")==0 || temp.compareTo("9")==0 || temp.compareTo(".")==0)
    {
    num+=temp;
    i++;
    }
    expression.substring(0,num.length());
    if (num.indexOf(".")==-1)
    num+=".0";
    return Double.parseDouble(num);

    }

    public double evaluate()
    {
    if(expression.length()!=0)
    {double num1=first;
    double ans=0;
    String operation=expression.substring(0,1);
    expression=expression.substring(1);
    double num2=number();
    if(operation.compareTo("+")==0)
    ans= (num1+num2);
    if(operation.compareTo("-")==0)
    ans= (num1-num2);
    if(operation.compareTo("*")==0)
    ans= (num1*num2);
    if(operation.compareTo("/")==0)
    ans= (num1/num2);
    if(operation.compareTo("%")==0)
    ans= (num1%num2);
    return ans;
    }
    else
    {return 0.0;}
    }

    public String getExpression()
    {return expression;}


    }

    import javax.swing.JOptionPane;
    public class calculator
    {
    public static void main(String[]args)
    {
    double answer = 0.0;
    String s=JOptionPane.showInputDialog("Enter expression","");
    calculate calc = new calculate(s);
    while(calc.getExpression().length()!=0)
    {answer += calc.evaluate();}
    JOptionPane.showMessageDialog(null,"The answer is" + answer);

    }
    }

    Btw could a mod tell me why I have to log in every time I navigate to a new page in the forum?: :mad:

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Please use CODE tags when posting code. Your code is very difficult to read when it is not formatted properly.
    Java Code:
    public class calculate
    Start your class name with an upper case letter, and make it a noun rather than a verb. This means that Calculator is a much better class name than calculate. Method names should be verbs -- calculate is not bad as a method name.
    Java Code:
    {
        private String expression;
        private double first;
    
        public calculate(String s)
        {
            expression = s;
            first = number();
        }
    Choose a better parameter name than s -- it's meaningless.
    Java Code:
        public double number()
        {
            String num = "";
            int i = 1;
            String temp = expression.substring(i - 1, i);
            while (temp.compareTo("1") == 0 || temp.compareTo("2") == 0 || 
                     temp.compareTo("3") == 0 || temp.compareTo("4") == 0 || 
                     temp.compareTo("5") == 0 || temp.compareTo("6") == 0 || 
                     temp.compareTo("7") == 0 || temp.compareTo("8") == 0 ||  
                     temp.compareTo("9") == 0 || temp.compareTo(".") == 0)
    temp.equals("1") is much simpler than temp.compareTo("1") == 0. Better yet, replace all that code with "0123456789.".contains(temp). (Better style would be to put that string in a constant.)
    Java Code:
            {
                num += temp;
                i++;
            }
    This is where your endless loop is. You increment i, but you never get another character from your expression String (the initial substring() call is before the while statement).
    Java Code:
            expression.substring(0, num.length());
            if (num.indexOf(".") == -1)
                num += ".0";
    Don't get into this habit. If you are doing a newline after your if condition, then use curly braces too.
    Java Code:
            return Double.parseDouble(num);
        }
    
        public double evaluate()
        {
            if (expression.length() != 0)
            {
                double num1 = first;
                double ans = 0;
                String operation = expression.substring(0, 1);
                expression = expression.substring(1);
                double num2 = number();
                if (operation.compareTo("+") == 0) // use .equals like we said earlier
                    ans = (num1 + num2); // don't need these parens
                if (operation.compareTo("-") == 0)
                    ans = (num1 - num2);
                if (operation.compareTo("*") == 0)
                    ans = (num1 * num2);
                if (operation.compareTo("/") == 0)
                    ans = (num1 / num2);
                if (operation.compareTo("%") == 0)
                    ans = (num1 % num2);
                return ans;
            }
            else
            {return 0.0;}
        }
    
        public String getExpression()
        {
            return expression;
        }
    }
    Java Code:
    import javax.swing.JOptionPane;
    
    public class calculator
    OK, this is why you didn't call your other class Calculator. Still, use a noun of some kind. Call it CalculatorEngine or something.
    Java Code:
    {
    
        public static void main(String[]args)
        {
            double answer = 0.0;
            String s = JOptionPane.showInputDialog("Enter expression","");
            calculate calc = new calculate(s);
            while (calc.getExpression().length() != 0)
            {
                answer += calc.evaluate();
            }
            JOptionPane.showMessageDialog(null, "The answer is" + answer);
        }
    }

  3. #3
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    I inserted a statement

    temp=expression.substring(i-1,i);

    into the loop. I got a runtime error about invalid parameters for the substring() method. It was why I had to remove it in the first place :|

    -Bayan-

Similar Threads

  1. Replies: 1
    Last Post: 03-30-2010, 10:23 PM
  2. Running a Java Program
    By Valkyrie in forum New To Java
    Replies: 13
    Last Post: 11-05-2009, 04:43 AM
  3. Error running java program using URL
    By gio123bg in forum New To Java
    Replies: 6
    Last Post: 06-30-2009, 07:26 PM
  4. error running java program
    By bdasilva in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 02:46 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:16 AM

Posting Permissions

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