This is a project for class where I have to find the pace per mile needed to complete a certain distance at a certain time however it outputs something like 0:5:31.0 I'm having trouble casting the second's value to an int. Also, it's not required but how would I go about replacing the if else statements with a switch statement, couldn't really figure that out either because it's a string.
Thanks.
Note when trying to do double PaceSec = (int) TotalS / miles; it tells me that casting int is redundant and it doesn't change the output value at all. Also, I can't cast int to miles because it would change the time.
Thanks.
Code
import javax.swing.JOptionPane;
public class RaceTime {
public static void main (String [] args){
String Distance = JOptionPane.showInputDialog(null, "Enter your distance '5K', '10K', 'Half Marathon', or 'Full Marathon'.");
String Time = JOptionPane.showInputDialog(null, "Enter the time you would like to complete it in the format hh:mm:ss");
//String Hour = Time.substring(0, Time.indexOf(":"));
int Hour = Integer.parseInt(Time.substring(0, Time.indexOf(":")));
int Minute = Integer.parseInt(Time.substring(Time.indexOf(":") + 1, Time.lastIndexOf(":")));
int Second = Integer.parseInt(Time.substring(Time.lastIndexOf(":") + 1));
int HourS = Hour * 3600; // /24
int MinuteS = Minute * 60;
int TotalS = HourS + MinuteS + Second;
double miles = 0;
if (Distance.equalsIgnoreCase("5K")){
miles = 3.1;
}else if (Distance.equalsIgnoreCase("10K")){
miles = 6.2;
}else if (Distance.equalsIgnoreCase("Half Marathon")){
miles = 13.1;
}else if (Distance.equalsIgnoreCase("Full Marathon")){
miles = 26.2;
}
double PaceSec = TotalS / miles;
int PaceMin = (int) PaceSec / 60;
PaceSec = (int) PaceSec % 60;
int PaceHour = PaceMin / 60;
PaceMin = PaceMin % 60;
JOptionPane.showMessageDialog(null, "Your pace per mile needs to be " + PaceHour + ":" + PaceMin + ":" + PaceSec + " to complete " + Distance + " in " + Time);
}
}Note when trying to do double PaceSec = (int) TotalS / miles; it tells me that casting int is redundant and it doesn't change the output value at all. Also, I can't cast int to miles because it would change the time.


