|
||
|
|||
|
I was assigned the task of making a program that outputs the number of minutes that have passed since midnight. The program reads the input from the user as h:m:ind, where h is the hour (must be >0 and <=12), m is the minuted (must be >= 0 and <60), and ind is either AM or PM.
Below is ten sample runs of what the program should give the following output. User input is in bold. Java Code
Enter h:m:AM or PM > test Non-numeric Data! Enter h:m:AM or PM > 15:5:AM Values out of range Enter h:m:AM or PM > 1:15:am 75 Enter h:m:AM or PM > 12:10:am 10 Enter h:m:AM or PM > 1:5:tm Invalid AM/PM indicator! Enter h:m:AM or PM > 11:50:am 710 Enter h:m:AM or PM > 12:10:pm 730 Enter h:m:AM or PM > 1:25:PM 805 Enter h:m:AM or PM > 11:50:pm 1430 Enter h:m:AM or PM > 12:55:am 55 Enter h:m:AM or PM > 5 Values out of range "Values out of range!" if h or m is outside its legal range. "Missing colon!" if a colon is missing. "Non-numeric data!" if h or m is not an integer. "Invalid AM/PM indicator" if the indicator is neither AM nor PM (upper or lower) The hardest part of the assignment is that this program must be executed without any validation and without using any if statements, switch statements or similar construct. Below is my programs output. Incorrect outputs are underlined. Java Code
Enter h:m:AM or PM > test Non-numeric Data! Enter h:m:AM or PM > 15:5:AM Values out of range Enter h:m:AM or PM > 1:15:am 75 Enter h:m:AM or PM > 12:10:am 10 Enter h:m:AM or PM > 1:5:tm Invalid AM/PM indicator! Enter h:m:AM or PM > 11:50:am 710 Enter h:m:AM or PM > 12:10:pm 730 Enter h:m:AM or PM > 1:25:PM 805 Enter h:m:AM or PM > 11:50:pm 1430 Enter h:m:AM or PM > 12:55:am 55 Enter h:m:AM or PM > 5 Missing Colon Perhaps there is a better way to throw invalid without using if statements. I thought of using a regex, i.e. Java Code
String regex = "(1[012]|[1-9]):[0-5][0-9]:(AM|PM|am|pm)"; Handling exceptions is a very hard/new topic for me so any help would be appreciated, thanks. Edit: I figured it out..
Last edited by Hax007; 08-12-2009 at 11:01 PM. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ymsg exception problem | maruf10 | Java Networking | 0 | 19-09-2009 06:05 PM |
| Help with ClassNotFound Exception | multicoder | Exceptions | 3 | 01-07-2009 07:58 PM |
| COM based component Project | jazz2k8 | Java Native Interface | 1 | 08-10-2008 03:54 AM |
| Exception handling | AnithaBabu1 | Exceptions | 6 | 27-08-2008 02:37 PM |
| Funny Java based animation | JavaPF | The Cafe | 1 | 07-06-2008 11:55 AM |
|
100 most searched terms
Search Cloud
|
| 2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? |