Results 1 to 4 of 4
Thread: Time Calculator Problem
- 09-17-2009, 09:05 PM #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:
This is what I have so far: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.
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.
-
One immediate problem that I see that will kill you every time:
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); // <=== watch this semicolon! { 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.Java Code:if (seconds < 60.0) { time = "0 minutes and" + seconds; }
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.
- 09-18-2009, 09:27 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
As a hint, '%' operator is your friend.
- 09-22-2009, 07:15 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Simple Calculator Display Problem :(
By jimbob in forum Java AppletsReplies: 4Last Post: 07-18-2009, 04:13 AM -
System time problem
By dswastik in forum CLDC and MIDPReplies: 0Last Post: 04-08-2009, 01:36 PM -
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12-19-2008, 11:58 PM -
Time-Date problem
By teo.danciu in forum New To JavaReplies: 5Last Post: 08-27-2008, 10:01 AM -
Calculator Problem. Thanks for helping! ^^
By clark_sandy in forum New To JavaReplies: 3Last Post: 07-06-2008, 04:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks