public static double changeToSpeed(String time, int distance)
{
if(time.indexOf(":") == 1){
int minutes = time.charAt(0);
String sec = time.substring(3, 7);
double seconds = Double.parseDouble(sec);
double totalTime = 60 * minutes + seconds;
double endSpeed = distance / totalTime;
return endSpeed;
}
else if(time.indexOf(":") == 2){
String min = time.substring(0, 2);
String sec = time.substring(3, 7);
double minutes = Double.parseDouble(min);
double seconds = Double.parseDouble(sec);
double totalTime = 60 * minutes + seconds;
double endSpeed = distance / totalTime;
return endSpeed;
}
else if(time.indexOf(":") == -1){
int minutes = 0;
double seconds = Double.parseDouble(time);
double totalTime = 60 * minutes + seconds;
double endSpeed = distance / totalTime;
return endSpeed;
}
}
I'm trying to get different returns based on where the ":" is in the String time. This gives me a problem with having no return value in the main part of the method, but when i do that it gives me a different error saying I have too many return statements. I needs the helps.
The problem at the moment is that your method doesn't return anything if time.indexOf(":")
doesn't return 1, 2 or -1. What do you want to do in that situation? You may want to throw an exception, for example. You should work out what you want to happen in that situation - and then you can work out how to implement it.
I'd also suggest one refactoring to start with: most of this method is to do with parsing a time; you're then doing the same thing with the distance and the parsed time. So extract the time parsing to a separate method:
public static double changeToSpeed(String time, int distance) {
double totalTime = parseTime(time);
return distance / totalTime;
}
private static double parseTime(String time) {
// Now you *only* have to deal with time in here
}
Additionally, this doesn't do what you expect it to:
int minutes = time.charAt(0);
... that will give 49 for '1' for example. And when you're using Double.parseDouble
for the minutes part of minutes:seconds, do you really want a double
, or do you actually want an int
? Are you really expecting something like .5:20
to mean 50 seconds?
Finally, consider that the only difference between your ".:...." and "..:...." is how you handle the minutes, really. In both cases you can just parse an integer:
int colon = time.indexOf(':');
// If we don't have a colon, just assuming it's minutes:seconds in some form
if (colon != -1) {
int minutes = Integer.parseInt(time.substring(0, colon));
double seconds = Double.parseDouble(time.substring(colon + 1));
return minutes * 60 + seconds;
} else {
return Double.parseDouble(time);
}
Now that assumes that you want 100:30.5 to be a valid time. If you really only want a colon at position 1 or 2, you should check that:
if (colon == 1 || colon == 2) {
int minutes = Integer.parseInt(time.substring(0, colon));
double seconds = Double.parseDouble(time.substring(colon + 1));
return minutes * 60 + seconds;
} else if (colon == -1) {
return Double.parseDouble(time);
} else {
throw new /* some appropriate exception type */
}
See more on this question at Stackoverflow