Results 1 to 11 of 11
  1. #1
    jacques5309 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Phone number stringtokenizer problem

    how would I go about writing a class that uses the stringtokenizer to identify the three parts of a phone number? (using three methods)

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Why must you have 3 methods?
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    jacques5309 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    I'm not sure the reasoning, probably since we are just starting with the stringtokenizer.

    This is the exact question I was given:

    1. Write a class that uses the StringTokenizer class to identify
    the parts of a phone number. Assume that the format of the phone
    number is (nnn) nnn-nnnn. Example (860) 222-3344. The class should
    have at least three public methods: one returning the area code,
    one returning the exchange and one returning the extension.

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Do you know how to set up the 3 methods at least? I'm not sure how much you already know about Java. Also, posting some code you already have would help a lot.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    jacques5309 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    I just really have no clue how to write it. I'm starting at ground zero trying to figure it out.

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    This will help get you started with writing the methods: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    I really don't think we can help until you have some sort of code posted that we can look at.
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    jacques5309 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Something like :

    StringTokenizer tokenizer = StringTokenizer("() -");


    String areaCode = tokenizer.nextToken();
    String exchange = tokenizer.nextToken();
    String extension = tokenizer.nextToken();

    but creating a class for each areacode exchange and extension, so it can be used in other java applications.

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Here is something to get you started. I've made comments in the code for you to take a look at to make sure you understand each part.

    Java Code:
    import java.util.StringTokenizer;
    
    public class tokens 
    {
    	public static void main(String[] args) 
    	{
    		String phone = "(555) 454-3456"; // The phone number.
    		String areaCode = getAreaCode(phone); // Gets the area code from a method.
    		System.out.print(areaCode); // Prints just the area code to the screen.
    	}
    	
    	public static String getAreaCode(String phone)
    	{
    		// Creates tokenizer for the phone number that grabs
    		// anything in between parenthesis (). Which is what
    		// we want.
    		
    		StringTokenizer areaCode = new StringTokenizer(phone, "()");
    		return areaCode.nextToken(); // Returns just the area code in
    		                             // between the parenthesis.
    	}
    }
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    jacques5309 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    hey thanks a lot, that really got the ball rolling!

  10. #10
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Phone number stringtokenizer problem

    hi i have to write exactly the same class and i'm having a lot of trouble
    can someone explain to me how should i do it ?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Phone number stringtokenizer problem

    Quote Originally Posted by artur View Post
    hi i have to write exactly the same class and i'm having a lot of trouble
    can someone explain to me how should i do it ?
    Please don't hijack someone else's old thread. Instead go to the New to Java forum and start your own new thread where you can ask this question. Provide pertinent details such as your code and your exact problems including any and all error messages.

    And shoot, the answer to the fellow's question is already posted in this thread!

    Locking.

Similar Threads

  1. number problem
    By click66 in forum New To Java
    Replies: 7
    Last Post: 10-11-2010, 03:27 PM
  2. Phone number Program ..
    By Sary in forum New To Java
    Replies: 9
    Last Post: 03-17-2010, 07:15 PM
  3. Problem with delim in StringTokenizer
    By acp26b in forum New To Java
    Replies: 2
    Last Post: 01-15-2009, 02:44 AM
  4. Replies: 2
    Last Post: 10-14-2008, 09:32 PM
  5. Menu on phone number option of a list
    By Poonam in forum CLDC and MIDP
    Replies: 7
    Last Post: 01-31-2008, 01:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •