File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
Lisa Bergeron
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Thanks for showing this "greenhorn" more than one way to approach this problem.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic