Results 1 to 4 of 4
  1. #1
    ktisallred is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

  4. #4
    ktisallred is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    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!

Similar Threads

  1. Simple Calculator Display Problem :(
    By jimbob in forum Java Applets
    Replies: 4
    Last Post: 07-18-2009, 04:13 AM
  2. System time problem
    By dswastik in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-08-2009, 01:36 PM
  3. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 11:58 PM
  4. Time-Date problem
    By teo.danciu in forum New To Java
    Replies: 5
    Last Post: 08-27-2008, 10:01 AM
  5. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 04:01 PM

Posting Permissions

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