I'm fairly new to java and need help. I need to show time required to travel somewhere by having the user input Speed and Distance traveled. And the outcome be a floating point value.

lets say they put in 60 mph and 148 miles
I Can't seem to get the decimals right...or something is wrong with my conversion, I have tried a few thing but ill paste the base code I was using and see if anyone can help out.

If i take 148 miles and divide by 60 i get 2.4666666666

But i don't get that as my result. It should read 2 hours 28minutes

``````/* File Name: TimeRequired.java
* Date:
* Name:
* Version:
* Description: This will tell you time required based off
* of Distance = Speed*Time
*/

import java.util.Scanner;

public class TimeRequired
{
public static void main(String[] args)
{
// Variables
int speed;
int distance;
float time;
float hour;
float minutes;
float seconds;

Scanner scan = new Scanner(System.in);

// Speed of Traveler
System.out.print("Enter the Speed in MPH ");
speed = scan.nextInt();

// Distance Traveled
System.out.print("Enter the Distance Traveled ");
distance = scan.nextInt();

// calculations
the calculations i have tried keep coming up with wrong ones..

//Display
System.out.println("Time in Hours: "+hour);
System.out.println("Time in Minutes: "+minutes);
System.out.println("Time in Seconds: "+seconds);
System.out.println("The Total Time is: "+hour+":"+minutes+":"+seconds);

}
}
``````

The value you get is in hours: 2.4666 hours.
How many minutes in an hour?
If you multiply the fractional part by the minutes per hour you'll get the number of minutes.
See the Math class for rounding methods.

Be a part of the DaniWeb community

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