Can anyone help me please? i need to create a program in which you enter a distance and the program will tell you how long it will take to get there in hours minutes and seconds.

My trouble is that they all display independently and not seperately ... for example ... i want it to display "1 hour 20mins 4 seconds" instead of "1.20 hours 80 mins" and how ever many in seconds.


thank you in advance for your help :)

public class Travel{

    public static void main (String [] argv){
        
        //variables
        final float kph =85;
              float distance;
              float sec;
              float min;
              float hour;
              float subtot;
             // float total;
            //  float grandtot;
              
              // prints out user prompt 
              System.out.println("Please Enter the Distance: ");
                distance = UserInput.readFloat ();
                
                // formula to work out length of travel 
                subtot = distance/kph;
               // hour = subtot%10;
                                           
               // total = distance/kph*60;
               // min = total%10;
                
               // grandtot = distance/kph*60*60;
               // sec = grandtot%10;
               
               min = subtot*60;
               hour = min % 60;
               sec = min*60;
               //total= subtotal*60;
                               
               // total = distance/kph;
                
                //timeInMin = 
              
               System.out.println("Time in Hours: "+hour); 
               System.out.println("Time in Minutes: "+min); 
               System.out.println("Time in seconds: "+sec);
               System.out.println("The Total Time is: "+hour+":"+sec+":"+min);
        
    }
}

For staters, you don't need to be using floats for hour, sec, min and this is where a lot of your issues will be starting.

If you avoid decimal multiplication / division, you will probably get results closer to what you want. e.g. 1.20 = 1 in int (I am referring to your hours).

After you calculate hours, you need to take this into account when calculating the minutes. After you get the minutes you need to take this & hours into account when you calculate the seconds.

Hope this helps...

I think that this if kph means speed than distance/kph means time in hours you don't need variable hours. you can simlply write

System.out.println("Time in Hours: "+subtot);

In Physics everything is measured in meters and seconds.
So after you get the speed and distance, Convert both to meters/sec and meters. After you do the division you will have a clean value in seconds which can be easily converted to minutes and hours.
If the final value has decimal points: 65.33 seconds then keep them at the seconds value or round them or have another variable for miliseconds:
1 minute and 5.33 seconds or
1 minute and 5 seconds or
1 minute and 5 seconds and 330 milliseconds

Thank you for your posts!! it is really appreciated :)

i will give it a go and let you know how it turns out.

thanks again :D

I tried using the modulo and i still get decimals.

I also tried changing my some of my floats to integers, but my compiler will not let it compile as it says "Possible Loss of Precision"

is there anyway around this?

p.s I changed the hour, min and sec to int

How about some code

... for example ... i want it to display "1 hour 20mins 4 seconds" instead of "1.20 hours 80 mins" and how ever many in seconds. ...

min = subtot*60;
               hour = min % 60;
               sec = min*60;
min = subtot * 60;
if ( min >= 60){
  hour = min%60;
}
else{
  hour = 0;
}

if (hour != null){
  min -= (hour*60);
}

that should fix that. once you've taken them as hours, subtract the right amount of minutes from min to get the correct answer

thanks everyone, but i have solved it :)

public class Travel {

    public static void main(String[] argv) {

    // put your local declarations here
    final double SPEED = 85;
    double Distance = 0.0;
    long Seconds;
    long Minutes;
    long Hours;
        
    
    // Prompt and read the distance
    UserInput.prompt("Type the travel distance: ");

    Distance = UserInput.readDouble();
        
    
    // Compute and print
    Seconds = (long) ( Distance * 3600 / SPEED );

    System.out.println ( "To travel " + Distance + " takes " + Seconds
        + " seconds" );

    Minutes = Seconds / 60;
    Seconds %= 60;
    Hours = Minutes / 60;
    Minutes %= 60;

    System.out.println ( "To travel " + Distance + " takes " 
        + Hours + " hours " + Minutes + " minutes " + Seconds
        + " seconds" );

    } // end of main

} // end class

thank you again for all your help.

much appreciated.

Nice work, you can mark the thread as solved so that people know too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.