ITEC 120 LAB 24 | Back to Lab index page |
Exception handling
Write a program to read in course number until the user enters DONE.
Course numbers consist of 4 letters and 3 digits, for example: ITEC120 or ECON105
Create a new exception called InvalidCourseNumberException which gets thrown if the course number does not consist of 4 letters and 3 digits. Catch this exception and give the user an appropriate error message and continue to process course numbers..
When you check to see if the characters in the course numbers are letters or digits, you will need to use the String method charAt, and, you are in danger of throwing a StringIndexOutOfBoundsException if there is not a character at the position you are looking at. Catch the StringIndexOutOfBoundsException and give the user an appropriate error message and continue to process course numbers.
Sample run:
Program outputs: | Enter a course number (DONE to quit): |
User inputs: | ITEC120 |
Program outputs: | Enter a course number (DONE to quit): |
User inputs: | ITEC1 |
Program outputs: | CourseNumber
not long enough. Enter a course number (DONE to quit): |
User inputs: | ITECxxx |
Program outputs: |
Course number must be 4 digits
and 3 numbers. |
User inputs: | BIOL101 |
Program outputs: | Enter a course number (DONE to quit): |
User inputs: | DONE |
Once you have your program working, show your lab to the Peer Instructor to be checked off.