1. Member
Join Date
Sep 2009
Posts
2
Rep Power
0

## 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:
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:

Java 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 ".");

}
}```
Last edited by ktisallred; 09-17-2009 at 09:07 PM.

2. One immediate problem that I see that will kill you every time:
Java 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:
Java 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.
Last edited by Fubarable; 09-17-2009 at 10:58 PM.

3. Moderator
Join Date
Apr 2009
Posts
10,484
Rep Power
16

4. Member
Join Date
Sep 2009
Posts
2
Rep Power
0
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!

#### Posting Permissions

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