Check11A - hours = hours % H; minutes = minutes % M; String...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.Scanner; import java.io.PrintStream; import type.lib.ToolBox; i public class Check11A { public static void main(String[] args) { PrintStream out = System.out; Scanner in = new Scanner(System.in); out.print("Enter h:m:AM or PM > "); try { String buffer = in.nextLine(); final int H = 12, M = 60; String token1 = buffer.split(":")[0]; int hours = Integer.parseInt(token1); ToolBox.crash(hours > H || hours <= 0, "Values out of range!"); String token2 = buffer.split(":")[1]; int minutes = Integer.parseInt(token2); ToolBox.crash(minutes >= M, "Values out of range!");
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hours = hours % H; minutes = minutes % M; String token3 = buffer.split(&quot;:&quot;)[2].toUpperCase(); ToolBox.crash(!token3.matches(&quot;[AP]M&quot;), &quot;Invalid AM/PM indicator!&quot;); final String AMPM = &quot;00AM12PM&quot;; int check = AMPM.lastIndexOf(token3), a = Integer.parseInt(AMPM.substring(check-2, check)); out.println(M * (hours + a) + minutes); } catch (ArrayIndexOutOfBoundsException e) { out.println(&quot;Missing colon!&quot;); } catch (NumberFormatException e) { out.println(&quot;Non-numeric Data!&quot;); } catch (RuntimeException e) { out.println(e.getMessage()); } } }...
View Full Document

Ask a homework question - tutors are online