Results 1 to 3 of 3
Thread: Program running indefinitely
- 04-27-2010, 07:43 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
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:
- 04-27-2010, 09:10 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Please use CODE tags when posting code. Your code is very difficult to read when it is not formatted properly.
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:public class calculate
Choose a better parameter name than s -- it's meaningless.Java Code:{ private String expression; private double first; public calculate(String s) { expression = s; first = number(); }
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: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)
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:{ num += temp; i++; }
Don't get into this habit. If you are doing a newline after your if condition, then use curly braces too.Java Code:expression.substring(0, num.length()); if (num.indexOf(".") == -1) num += ".0";
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; } }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:import javax.swing.JOptionPane; public class calculator
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); } }
- 04-27-2010, 09:22 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
Changes in code not showing up when running program!?
By jchsu in forum New To JavaReplies: 1Last Post: 03-30-2010, 09:23 PM -
Running a Java Program
By Valkyrie in forum New To JavaReplies: 13Last Post: 11-05-2009, 03:43 AM -
Error running java program using URL
By gio123bg in forum New To JavaReplies: 6Last Post: 06-30-2009, 06:26 PM -
error running java program
By bdasilva in forum New To JavaReplies: 1Last Post: 06-29-2009, 01:46 AM -
Call a main method from within a running program
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks