My program first prompts the user to choose which conversion they would like: "24-Hour to 12-Hour" and vice-versa.
Then if the user selects the "12-Hour to 24-Hour", and enters the time... the computer does not recognize the suffix (pm/am) at the end of the time.
I have coded the program to recognize the space at the end of the time and determine which suffix the user entered in order to either add 12 if the user entered "pm" or keep the hour the same if the user entered "pm". With the exclusion being "12:00 am" which subtracts 12 from the hour.
Below is a snippet from "Convert12To24.java" which I think is the problem.
try { // Begin try.
int colon = time.indexOf(':'); // Create a String index called 'colon'.
int space = time.indexOf(' '); // Create a String index called 'space'.
if (colon != 2 || space != 5) { // If colon is not present.
TimeFormatException tfe = new TimeFormatException(
"You entered " + time + "\nFormat must be H:MM XX");
throw tfe; // Throw the TimeFormatException.
} // End if.
String hourStr = time.substring(0, colon);
String minStr = time.substring(colon + 1, time.length());
String pmStr = time.substring(space + 1, time.length());
if (minStr.length() != 2 )
throw new TimeFormatException(
"You forgot the leading zero for " + minStr);
if (minStr == "") // If user enters strings.
throw new TimeFormatException("You entered " + hourStr + ":"
+ minStr + "\nFormat must be HH:MM");
if (pmStr.length() != 2)
throw new TimeFormatException("You forgot the suffix for "
+ pmStr); // Throw the TimeFormatException.
Scanner hourScan = new Scanner(hourStr);
Scanner minScan = new Scanner(minStr);
hour = hourScan.nextInt();
min = minScan.nextInt();
pm = pmStr;
} // End try.
catch (InputMismatchException e) { // Begin catch.
throw new TimeFormatException("\nPlease, do not enter words...");
} // End catch.
TimeFormatException.java
class TimeFormatException extends Exception { // Begin class TimeFormatException.
private static final long serialVersionUID = 1L; // Serialize.
public TimeFormatException(String msg) { // Begin single parameter constructor.
super(msg); // Pass the Exception class.
} // End single parameter constructor.
public TimeFormatException(String msg, String msg1) { // Begin two parameter constructor.
super(msg1 + msg); // Pass the Exception class.
} // End two parameter constructor.
} // End class.
Convert12To24.java
import java.util.InputMismatchException; // Import InputMismatchException class.
import java.util.Scanner; // Import Scanner class.
public class Convert12To24 {
public String convertTime(String time) throws TimeFormatException { // Begin method convertTime.
int hour = 0; // Initialize hour.
int min = 0; // Initialize minutes.
String pm = ""; // Initialize suffix.
time = time.trim(); // Trim time into substrings.
try { // Begin try.
int colon = time.indexOf(':'); // Create a String index called 'colon'.
int space = time.indexOf(' '); // Create a String index called 'space'.
if (colon != 2 || space != 5) { // If colon is not present.
TimeFormatException tfe = new TimeFormatException(
"You entered " + time + "\nFormat must be H:MM XX");
throw tfe; // Throw the TimeFormatException.
} // End if.
String hourStr = time.substring(0, colon);
String minStr = time.substring(colon + 1, time.length());
String pmStr = time.substring(space + 1, time.length());
if (minStr.length() != 2 )
throw new TimeFormatException(
"You forgot the leading zero for " + minStr);
if (minStr == "") // If user enters strings.
throw new TimeFormatException("You entered " + hourStr + ":"
+ minStr + "\nFormat must be HH:MM");
if (pmStr.length() != 2)
throw new TimeFormatException("You forgot the suffix for "
+ pmStr); // Throw the TimeFormatException.
Scanner hourScan = new Scanner(hourStr);
Scanner minScan = new Scanner(minStr);
hour = hourScan.nextInt();
min = minScan.nextInt();
pm = pmStr;
} // End try.
catch (InputMismatchException e) { // Begin catch.
throw new TimeFormatException("\nPlease, do not enter words...");
} // End catch.
if (hour > 12 || min > 59)
throw new TimeFormatException("\nYou entered " + hour + ":" + min
+ "\nHour <= 12 and Minute < 60"); // Throw
// TimeFormatException.
else if (hour <= 0 || min < 0)
throw new TimeFormatException("\nYou entered " + hour + ":" + min
+ "\nHour >= 0 and Minute >= 0"); // Throw
// TimeFormatException.
else if (!pm.equalsIgnoreCase("am") && !pm.equalsIgnoreCase("pm"))
throw new TimeFormatException("\nYou entered " + hour + ":" + min
+ " " + pm + "\nTime must end in 'pm' or 'am'"); // Throw
// TimeFormatException.
else {
if (pm.equalsIgnoreCase("pm")) {
if (hour <= 11 && min <= 59)
hour = hour + 12;
} else if (pm.equalsIgnoreCase("am")) {
if (hour == 12 && min >= 0)
hour = hour - 12;
}
}
String formattedTime = String.format(
"That time is the same as:\n%02d:%02d\n", hour, min); // Compose
// the
// response.
return formattedTime; // Return calculated time.
}
}
Convert24To12.java
import java.util.InputMismatchException; // Import InputMismatchException class.
import java.util.Scanner; // Import Scanner class.
public class Convert24To12 { // Begin class Convert24To12.
public String convertTime(String time) throws TimeFormatException { // Begin method convertTime.
int hour = 0; // Initialize hour.
int min = 0; // Initialize minutes.
String pm = ""; // Initialize suffix.
time = time.trim(); // Trim time into substrings.
try { // Begin try.
int colon = time.indexOf(':'); // Create an colon object of type integer.
/** If a colon is not present, throw exception. */
if (colon != 2) { // If colon is not present.
TimeFormatException tfe = new TimeFormatException( // Create a new TimeFormatException.
"You entered " + time + "\nFormat must be HH:MM"); // Display incorrect time.
throw tfe; // Throw the TimeFormatException.
} // End if.
String hourStr = time.substring(0, colon); // Define hour as anything before the colon.
String minStr = time.substring(colon + 1, time.length()); // Define minutes as anything after colon.
if (minStr.length() != 2)
throw new TimeFormatException("You forgot the leading zero for " + minStr); // Throw the TimeFormatException.
if (hourStr == "" && minStr == "") { // If user enters strings.
throw new TimeFormatException("You entered " + hourStr + ":"
+ minStr + "\nFormat must be HH:MM"); // Throw the TimeFormatException.
}
Scanner hourScan = new Scanner(hourStr); // Scan the String hour.
Scanner minScan = new Scanner(minStr); // Scan the String minutes.
hour = hourScan.nextInt(); // Convert String hour into an integer.
min = minScan.nextInt(); // Convert String minutes into an integer.
} // End try.
catch (InputMismatchException e) { // Begin catch.
throw new TimeFormatException("\nPlease, do not enter words..."); // Throw TimeFormatException.
} // End catch.
if (hour >= 24 || min > 59) // If input exceeds limit:
throw new TimeFormatException("\nYou entered " + hour + ":" + min
+ "\nHour < 24 and Minute < 60"); // Throw TimeFormatException.
else if (hour < 0 || min < 0) // If input is negative.
throw new TimeFormatException("\nYou entered " + hour + ":" + min
+ "\nHour >= 0 and Minute >= 0"); // Throw TimeFormatException.
else { // Begin else.
if (hour == 12) // If hour is 12:
hour = hour - 0; // Do nothing.
if (hour > 12) // If hour is greater than 12:
hour = hour - 12; // Subtract 12 from current hour.
else if (hour == 0) // If hour is 0:
hour = 12; // Set hour to 12.
pm = (hour >= 12) ? "pm" : "am"; // Set suffix.
} // End else.
String formattedTime = String.format("That time is the same as:\n%02d:%02d %s\n", hour, min,
pm); // Compose the response.
return formattedTime; // Return calculated time.
} // End convertTime.
} // End class.
TimeConversionTest.java
import java.util.InputMismatchException;
import java.util.Scanner; // Import Scanner class.
public class TimeConversionTest { // Begin class TimeConversionTest.
public static void main(String[] args) { // Begin main.
System.out.printf("Convert 24-Hour Time to 12-Hour Time!"); // Print Header.
Convert24To12 convert1 = new Convert24To12(); // Create a new conversion called convert..
Convert12To24 convert2 = new Convert12To24();
while (true) { // Begin while.
String converted = ""; // Initialize converted.
String theTime = "";
int choice = 0;
try { // Begin try.
boolean validInput = false;
System.out.println("Choose a conversion (Enter 1 or 2):");
System.out.println("(1) 24-Hour to 12-Hour.");
System.out.println("(2) 12-Hour to 24-Hour.");
System.out.println("(3) Quit.");
while (!validInput) {
try {
choice = (new Scanner(System.in)).nextInt();
validInput = true;
} catch (InputMismatchException e) {
System.out.println("Invalid input!");
}
}
switch (choice) {
case 1:
System.out.println("\nEnter time in 24-hour notation (HH:MM):\n" );
theTime = new Scanner(System.in).nextLine(); // Store user input as theTime.
converted = convert1.convertTime(theTime); // Calculate conversion using theTime.
case 2:
System.out.println("\nEnter time in 12-hour notation (H:MM XX):\n" );
theTime = new Scanner(System.in).nextLine(); // Store user input as theTime.
converted = convert2.convertTime(theTime); // Calculate conversion using theTime.
case 3:
System.out.println("Program ended successfully.");
System.exit(0);
default:
System.out.println("Choose an appropriate option!");
}
} // End try.
catch (TimeFormatException tfe) { // Begin catch.
System.out.println(tfe.getMessage());
} // End catch.
System.out.println(converted); // Print response.
repeat(); // Call repeat method.
} // End while.
} // End main.
public static void repeat() { // Begin method repeat.
System.out.println("Again? (y/n)"); // Prompt user if they wish to continue.
String confirm = (new Scanner(System.in)).nextLine(); // Get user input and compare with acceptable answers.
while (!confirm.equalsIgnoreCase("Y") && !confirm.equalsIgnoreCase("N")) { // Only accepts 'y' or 'n'.
System.out.println("Enter 'y' or 'n':"); // Re-enter correct input.
confirm = (new Scanner(System.in)).nextLine();
} // End while.
if (confirm.equalsIgnoreCase("N")) { // If user does not want to continue:
System.out.println("End of program"); // End program.
System.exit(0); // Terminate program.
} // End if.
} // End method repeat.
} // End class.
This post has been edited by x68zeppelin80x: 10 November 2010 - 08:51 PM

New Topic/Question
Reply



MultiQuote









|