I am creating a program that converts Standard Time to the 24-hour-clock. Unfortunately for me, my "if" statements when the user inputs an incorrect time do not seem to be working properly.
Here is my code (I have included all of it because I feel like this error may be caused by another lower line):
public class StandardTime {
static String amPM;//will hold am/pm for standard time
static String traditionalTime;//will store traditional time from user
static int hours;//will store hours and minutes
static int min1, min2;
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// user input
int tryAgain = 1;//will initial do-while loop
System.out.println("Standard Time to Traditional Time Converter");
System.out.println("===========================================");
do {
System.out.println();
System.out.println("Input a time in Standard Form (HH:MM:SS):");
String standardTime = br.readLine();//user inputs time in standard form
System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);
do {
if (standardTime.length() != 8) {
System.out.println("Invalid time entered.");
System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
standardTime = br.readLine();//user inputs time in standard form
System.out.println();
}
else if (min1 >= 6)
{
System.out.println("Invalid time entered. Please do not input a minute time over 59.");
System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
standardTime = br.readLine();//user inputs time in standard form
System.out.println();
}
} while ((standardTime.length()) != 8 || (min1 >= 6));
//method declaration
convertToTraditional(standardTime); // call the coversion method
System.out.println(standardTime + " is equivalent to " + hours + ":" +min1+min2+ " " + amPM + ".");
System.out.println();
System.out.println("Enter 1 to try again or any other number to exit the program.");
tryAgain = Integer.parseInt(br.readLine());//user decides to try again
} while (tryAgain == 1);//will repeat if user enters 1
}//closes main body
public static void convertToTraditional(String standardTime) {
String hour = standardTime.substring(0, 2); //captures substring of hours in user's input
hours = Integer.parseInt(hour); //converts substring to INT value
int pmHour = hours;
if (12 == hours) { //creates a statement for 12PM (noon)
hours = pmHour;
amPM = "PM";
} else if (hours > 12 && hours <= 23) { //if the time is not equal to noon or midnight
hours = hours - 12;
amPM = "PM";
} else if (hours == 0) { //12 midnight
hours = hours + 12;
amPM = "AM";
} else if (hours == 24) { //also another way to express 12 midnight on the 24 hour clock
hours = hours - 12;
amPM = "AM";
} else { //else if the hours are between midnight and 11am
hours = pmHour;
amPM = "AM";
}
}
}
The error specifically comes up on the if "standardTime.length() != 8." I want the user to type it in using the following format "HH:MM:SS". This is the error I receive when, for example, I type in 1:2:3...
Exception in thread "main" java.lang.NumberFormatException: For input string: ":3"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at standardtime.StandardTime.main(StandardTime.java:35)
Java Result: 1
Thank you for any help.