• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome! Thanks for showing this "greenhorn" more than one way to approach this problem.
reply
    Bookmark Topic Watch Topic
  • New Topic