Time Calculator Problem

• 09-17-2009, 09:05 PM
ktisallred
Time Calculator Problem
Hello all! I just started taking Java this year and am quite behind because of my grandmother passing away. I need some help finishing this problem and making a working program.

This is what the problem states:
Quote:

Write a program that asks the user to enter a number of seconds.
• There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds.
• There are 3600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to 3600, the program should display the number of hours in that many seconds.
• There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86400, the program should display the number of days in that many seconds.

This is what I have so far:

Code:

```import javax.swing.*; public class TimeCalculator {         public static void main(String args[])         {                 // Define variables.                 double seconds;                         String input, time;                         // Get input from user.                 input = JOptionPane.showInputDialog("How many seconds?");                 seconds = Double.parseDouble(input);                         //Determine the time in seconds.                 if (seconds < 60.0);                 {                         time = "0 minutes and" + seconds;                 }                                 //Determine the time in a minutes.                 else if (seconds >= 60.0)                 {                         time = "minute";                 }                                 //Determine the time in an hour.                 else (seconds < 3600.0)                 {                         time = "hour";                 }                         //Display results.                 JOptionPane.showMessageDialog(null, "Time equals: " + time ".");                 } }```
• 09-17-2009, 10:53 PM
Fubarable
One immediate problem that I see that will kill you every time:
Code:

```if (seconds < 60.0);  // <=== watch this semicolon! {   time = "0 minutes and" + seconds; }```
The semicolon at the end of the first line above will "short-circuit" your if statement and will make it so that the code below will be called every time, regardless if seconds are less than 60 or not. The solution is to get rid of the semicolon here:
Code:

```if (seconds < 60.0) {   time = "0 minutes and" + seconds; }```
Next, looks like you'll have to do some math in here somewhere to return a numeric result. I think that you should be able to figure out how to do this.

There are other small errors sprinkled throughout your code. The error messages will tell you what the problem is, so try to use them to your advantage to help you figure out what needs fixing.

Finally, I think I speak for all when I say that we are terribly sorry for your loss. If you are indeed quite behind in your studies, please talk to your teachers about arranging some one-on-one tutoring. Much luck to you.
• 09-18-2009, 09:27 AM
Tolls
• 09-22-2009, 07:15 PM
ktisallred
Thanks! I hope I can get this going eventually. From class I've taken away that I should probably be using the && somewhere in here. I'll work on it! Thanks!