I'm working on a program that converts 24 hour time stamps into 12 hour time stamps. I managed to complete the conversion and loop it, but I need to code in an input validation that checks for incorrect inputs. An example of an incorrect input would be: "10:83" or "1):*2". Can someone show me how I can go about this using an Exception method? So far I have this:
public class conversion {
public static void timeChange() throws Exception {
System.out.println("Enter time in 24hr format");
Scanner sc = new Scanner(System.in);
String input1 = sc.nextLine();
DateFormat df = new SimpleDateFormat("HH:mm");
DateFormat df2 = new SimpleDateFormat ("hh:mm a");
Date date = null;
String timeOutput = null;
date = df.parse(input1);
timeOutput = df2.format(date);
System.out.println("in 12 hour format: " + timeOutput);
decision();
}
public static void decision() throws Exception {
System.out.println("Would you like to enter another time?");
Scanner sc2 = new Scanner(System.in);
String userChoice = sc2.nextLine();
while (userChoice.equalsIgnoreCase("Y")) {
timeChange();
}
System.exit(0);
}
public static void main(String[] args) throws Exception {
timeChange();
}
}