aspose file tools*
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Lisa Bergeron
Greenhorn

Joined: Nov 27, 2003
Posts: 7
I have not a clue how to deal with the phone number formatting part of the following problem - can anyone help??
Create a utility class with a method that accepts a phone number as a String and ensures that the formatting is nnn-nnn-nnnn. If the format is incorrect, generate a ´┐ŻBadPhoneNumberFormat exception.
So far I have created my exception:

import java.io.*;
public class BadPhoneNumberFormat extends IOException {
public String e = "Correct phone number format is:nnn-nnn-nnnn";
public BadPhoneNumberFormat() {}
public BadPhoneNumberFormat(String e){
super (e);
}
String readPhoneNo()throws BadPhoneNumberFormat
{
return e;
}
}
I have set up my utility class with a partial method:
public class PhoneNumberUtil {
static void phoneNumber(int a) throws BadPhoneNumberFormat {
if() ???
throw new BadPhoneNumberFormat();
System.out.println("Bad formatting");
else
System.out.println("Yeah!! Good formatting!!");
}
}
THEN I have to: look at Exceptions printStackTrace method and incorporate that into my code. I have been coding all week and am getting mighty fuzzy!
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

static void phoneNumber(int a) throws BadPhoneNumberFormat {
this method signature will force you to always return false. The phone number is supposed to be int format of "xxx-xxx-xxxx" but since you take an integer in as your parameter it can not contain the "-"'s so it will always be a bad phone number. If you change your method signature to
static void phoneNumber(String a) throws BadPhoneNumberFormat {
then you could do something like this:

that should help or at least give you a starting point to work from
Jamie
[ November 27, 2003: Message edited by: Jamie Robertson ]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

actually, if you are using jdk 1.4 then you could simplify the method by using regular expressions:

just another "cleaner" way to accomplish your goal
Jamie
[ November 27, 2003: Message edited by: Jamie Robertson ][/QB]
Lisa Bergeron
Greenhorn

Joined: Nov 27, 2003
Posts: 7
Awesome! Thanks for showing this "greenhorn" more than one way to approach this problem.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception Handling