Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

no error messages but program doesn't display required results

 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks,
I wrote the program below to calculate cost of phone call when the user enters values for hours, minutes and seconds. I have to convert hours and seconds to minutes so that i can calculate cost of phone call which is 10 cents per min. Code compiles without any errors but does not display the output. I would greatly appreciate any help.
Thanx,
kanaka
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're assuming that paint() will be called after each textfield is changed, or before the "calculate" handler runs. There's no reason to believe that.

Move the contents of "paint()" into a "calculate" method, and then call calculate() in the event handler, before using the variable totalValue.
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
You're assuming that paint() will be called after each textfield is changed, or before the "calculate" handler runs. There's no reason to believe that.

Move the contents of "paint()" into a "calculate" method, and then call calculate() in the event handler, before using the variable totalValue.

Ernest,
Thank you for replying. This is the change i did to the code. I think i understood what you meant. But still not getting the output.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ask yourself "Where does the variable 'hours' get set to equal the number of hours entered into the text field?"
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,
Thank you sooooo much. I figured what silly mistake i was doing. I am a beginner striving to do good programming. I got the output now. This is the code i had to add to fix the problem.

public int calculateTotal()
{
hours = Integer.parseInt(hourTF.getText());
mins = Integer.parseInt(minTF.getText());
secs = Integer.parseInt(secTF.getText());

totalMin = hours * 60 + mins + secs / 60;
totalCost = totalMin * 10;
return totalCost;
}
Thank again,
Kanaka
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Ask yourself "Where does the variable 'hours' get set to equal the number of hours entered into the text field?"


Ernest,
Thank you sooooo much. I figured what silly mistake i was doing. I am a beginner striving to do good programming. I got the output now. This is the code i had to add to fix the problem.

public int calculateTotal()
{
hours = Integer.parseInt(hourTF.getText());
mins = Integer.parseInt(minTF.getText());
secs = Integer.parseInt(secTF.getText());

totalMin = hours * 60 + mins + secs / 60;
totalCost = totalMin * 10;
return totalCost;
}
Thank again,
Kanaka
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic