The Artima Developer Community
Sponsored Link

Java Answers Forum
Seconds to hours

2 replies on 1 page. Most recent reply: May 28, 2004 9:24 AM by Nate Hultman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Nate Hultman

Posts: 8
Nickname: vtgunner14
Registered: May, 2004

Seconds to hours Posted: May 27, 2004 5:33 PM
Reply to this message Reply
Advertisement
I was wondering if anyone has ever had to do a program like this. I have to read a number in thru a dialog box and convert an amount of seconds into hours, minutes, and seconds. Its supposed to have a messagebox pop up that tells the time, then another dialog should come up that asks if a user would like to repeat the action. here is the code i have so far. Any help or suggestions would be greatly appreciated. Thank you.

package program1;
import java.io.*;
import java.util.*;
import javax.swing.*;
 
public class Program1 {
 
 
  public void calcHMS(int timeInSeconds) {
 
    long amount;
    char repeat;
    do{
      amount=1;
      String input = JOptionPane.showInputDialog("Enter Seconds ");
 
      int hours, minutes, seconds;
      hours = timeInSeconds / 3600;
      timeInSeconds = timeInSeconds - (hours * 3600);
      minutes = timeInSeconds / 60;
      timeInSeconds = timeInSeconds - (minutes * 60);
      seconds = timeInSeconds;
 
      JOptionPane.showMessageDialog(null, "The time is "+(hours + " hour(s) " + minutes + " minute(s) " + seconds + " second(s)"));
 
 
 
 
       input = JOptionPane.showInputDialog("Enter 'Y' to repeat, or 'N' to quit");
        repeat = input.charAt(0);
      }while (repeat == 'Y' || repeat =='y');
      System.exit(0);
   }
 
   public static void main(String[] args) {
     //String input;
     Program1 prog = new Program1();
       prog.calcHMS(52400);
    }
 }
 


Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Re: Seconds to hours Posted: May 28, 2004 12:42 AM
Reply to this message Reply
Here is ur code with some modifications to it. What was the parameter for? You need to use the time given by the user through the input box.
And always comment your code to exlpain to other users of the code what your thoughts are.

import javax.swing.*;
 
public class Program1
{
  	public void calcHMS()
  	{
  		char repeat;
  		
	    	do
	    	{
	      		int input = new Integer(JOptionPane.showInputDialog("Enter Seconds ")).intValue();
	 
	      		int hours, minutes, seconds;
	      
		      	hours = input / 3600;
		      	input = input - (hours * 3600);
		      	minutes = input / 60;
		      	input = input - (minutes * 60);
		      	seconds = input;
		 
		      	JOptionPane.showMessageDialog(null, "The time is "+(hours + " hour(s) " + minutes + " minute(s) " + seconds + " second(s)"));
 
		      	repeat = (JOptionPane.showInputDialog("Enter 'Y' to repeat, or 'N' to quit")).charAt(0);
		}
		while (repeat == 'Y' || repeat =='y');
	
	System.exit(0);
   	}
 
   	public static void main(String[] args)
   	{
    		Program1 prog = new Program1();
       		prog.calcHMS();
    	}
}

Nate Hultman

Posts: 8
Nickname: vtgunner14
Registered: May, 2004

Re: Seconds to hours Posted: May 28, 2004 9:24 AM
Reply to this message Reply
Thanks, The problem I was having was getting the value the user entered into the equation.

Flat View: This topic has 2 replies on 1 page
Topic: Help with Java Applet Needed Previous Topic   Next Topic Topic: Applet inside JEditorPane?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use