Results 1 to 3 of 3
  1. #1
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Stringtokenizer phone number problem

    Hi. I have problem with my homework. The assignment is:

    "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."

    That's the code i wrote so far. It compiles but when i'm trying to run the program I get
    an error message.

    Java Code:
      import java.util.StringTokenizer;
    
        public class problem1{
        
         public static void main (String[] args) {  
         
    	   PHONETokenizer phone = new PHONETokenizer ("(860) 222-3344");
    	    
    	    System.out.println ("Exchange : " + phone.get_areacode());
            System.out.println ("Area Code: " + phone.get_exchange());
    	    System.out.println ("Extension : " + phone.get_extension());
             }
           }
           
        class PHONETokenizer {
           
           private String areaCode;
           private String exchange;
           private String extension;
           
           public PHONETokenizer (String phone) {
    	       
    	     StringTokenizer PHONE = new StringTokenizer (phone, "()");
    	      areaCode = PHONE.nextToken("");
    	      exchange = PHONE.nextToken();
    	      extension = PHONE.nextToken();
          }
    	    public String get_areacode () {
    		    return areaCode;
    	    }
    	    public String get_exchange () {
    		    return exchange;
    	    }
    	    public String get_extension () {
    		    return extension;
    	    }
           
    }
    And thats the error message:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(Unknown Source)
    at PHONETokenizer.<init>(problem1.java:26)
    at problem1.main(problem1.java:8)

    Thanks for help.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Stringtokenizer phone number problem

    NoSuchElementException means you are attempting to extract one more token when one doesn't exist. If you print out the result of countTokens you will see how many tokens there are. Alternatively you could use a while (tokenizer.hasMoreTokens()) to print out all the tokens and see exactly what tokens there are and where it differes from your assumption.

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

    Default Re: Stringtokenizer phone number problem

    Thank for help. I got it to work. In case someone needs help in future, here is the working code:

    Java Code:
      import java.util.StringTokenizer;
    
        public class problem1{
        
         public static void main (String[] args) {  
         
    	   PHONETokenizer phone = new PHONETokenizer ("(860) 222-3344");
    	    
    	    System.out.println ("Exchange : " + phone.get_areacode());
            System.out.println ("Area Code: " + phone.get_exchange());
    	    System.out.println ("Extension : " + phone.get_extension());
             }
           }
           
        class PHONETokenizer {
           
           private String areaCode;
           private String exchange;
           private String extension;
           
           public PHONETokenizer (String phone) {
    	       
    	     StringTokenizer PHONE = new StringTokenizer (phone);
    	      areaCode = PHONE.nextToken("()");
    	      exchange = PHONE.nextToken(")-");
    	      extension = PHONE.nextToken();
          }
    	    public String get_areacode () {
    		    return areaCode;
    	    }
    	    public String get_exchange () {
    		    return exchange;
    	    }
    	    public String get_extension () {
    		    return extension;
    	    }
           
    }

Similar Threads

  1. Random Phone number generator
    By elecleoalune in forum New To Java
    Replies: 15
    Last Post: 05-19-2014, 01:37 PM
  2. StringTokenizer phone number program
    By Erykah in forum New To Java
    Replies: 12
    Last Post: 11-22-2011, 08:16 AM
  3. Phone number stringtokenizer problem
    By jacques5309 in forum New To Java
    Replies: 10
    Last Post: 11-14-2011, 03:12 AM
  4. UK Phone Number regex validation?
    By ozzyman in forum New To Java
    Replies: 8
    Last Post: 04-12-2011, 10:15 AM
  5. Phone number Program ..
    By Sary in forum New To Java
    Replies: 9
    Last Post: 03-17-2010, 08:15 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
  •