Results 1 to 13 of 13
  1. #1
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default StringTokenizer phone number program

    Hello,

    I'm in a beginning java class & was assigned the following problem:
    Write a class using StringTokenizer class to identify parts of a phone number. Input format of phone number will be (nnn) nnn-nnnn. Example (123) 456-7890. The class should have at least three public methods: one each to return area code, exchange, and extension.

    I'll post in my code below - been getting a variety of errors, so as you'll see I've commented out a lot of the program, just trying to get it down to something basic which works. My book & instructor aren't easy to understand for help, so I rely almost entirely on internet resources to write all my assignments. This one hasn't been working from the beginnin, all I've done is trade errors for something else going wrong.

    Current errors are "cannot find symbol" on lines 67 & 77 - the returns for exchange & extension. Any help or advice would be appreciated.

    Thank you!


    // get utilities to use
    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.lang.Object;
    import java.lang.String;
    import java.io.*;

    public class ELShomework4a
    {
    static String exchange, extension;

    public static void main(String[] args)
    {
    // declare phone number string
    String phoneNum = "(123) 456-7890";

    // get & print numbers from methods
    String areaCode = getAreaCode(phoneNum);
    System.out.print(areaCode);

    String exchange = getExchange(phoneNum);
    System.out.print(exchange);

    String extension = getExtension(phoneNum);
    System.out.print(extension);

    // user input phone number
    //System.out.println( "Please enter a phone number in format \"(123) 456-7890\"." );
    //System.out.flush();
    //phoneNum = String.readLine();

    // check number format & ask for new input if incorrect
    //if(phoneNum == null||phoneNum.length() !=14)
    //{
    //System.out.println( "Please enter a phone number in format \"(123) 456-7890\"." );
    //System.out.flush();
    //}
    }

    // start separate methods for each section of #
    // begin with #s between () for area code

    public static String getAreaCode(String phoneNum)
    {
    // set delimiters, & exclude delimiter char from return
    StringTokenizer areaCode = new StringTokenizer(phoneNum, "()- ");
    {
    // set areaCode string
    //areaCode nextToken;

    // reports just the #s between parenthesis
    return areaCode.nextToken();
    }
    }

    public static String getExchange(String phoneNum)
    {
    // set delimiters to outline exchange part of string
    //String exchange = phoneNum.NextToken;
    {
    // reports just the #s between a space & dash
    return exchange.nextToken();
    }
    }

    public static String getExtension(String phoneNum)
    {
    // set delimeters for extension end of number
    //StringTokenizer extension = new StringTokenizer (phoneNum, ("-"));
    {
    // reports just the ending numbers after dash
    return extension.nextToken();
    }
    }
    }

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

    Default Re: StringTokenizer phone number program

    Variables exchange and extension are Strings. Does the string class have a nextToken method.

    Why do your getXXX methods have a parameter? The whole point of getter methods is to simply return a value (usually a instance variable). They do not need anything to be passed into the method.

  3. #3
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringTokenizer phone number program

    No, the StringTokenizer class has the nextToken method, but the way I see it being written was "public String nextToken()"
    When I tried it that way, I got even more errors, so I googled & messed with it some more - the current code returned the least errors. I'm confused by the fact that the areaCode isn't returning an error, though it's written exactly the same. Obviously, I don't understand what I'm doing well enough... which is why I'm asking for help. :-)

    I honestly don't know why I set up the getter methods that way. Must've been something I found in an example somewhere & applied incorrectly. I've never used a getter before & am pretty clueless, but I'll try to look it up & see what I did wrong. :-(

  4. #4
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringTokenizer phone number program

    If I remove the parameter from the getter statements, it returns an error.

    Can anyone please look at my code & give me some direction? I'm stuck!

    Thanks,

    Erykah

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

    Default Re: StringTokenizer phone number program

    I have looked at your code and I have given some direction but it appears you just want to be spoonfed. Well here is how your getExtension method should look:
    Java Code:
    public String getExtension() {
        return extension;
    }
    Elsewhere in your class (in the constructor or a method called by the constructor) is where you use the StringTokenizer to break the phone number down into the separate parts and assign the values to instance variables.

  6. #6
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringTokenizer phone number program

    I'm sorry, but I really don't understand how I'm giving you the impression that I'm asking to be spoon fed. I have put in a lot of work before posting, with lots of code to show for it, plus I explained above that I had thought about your response, yet still couldn't figure it out.

    It looks to me as though I do have the StringTokenizer used elsewhere in my class, and that is where I am getting the 2 errors. I don't understand because it's saying it "cannot find symbol" for return exchange.nextToken & return extension.nextToken, but areaCode is written the same exact way & not bringing up any errors. To a beginner, that's confusing. I'd expect to get errors for everything written the same way.

    I'm also unsure why the getExtension needs to be changed if it isn't returning an error. I'm just a student looking for help - my teacher snaps when she is asked, so I was hoping for a better response here. :-(

  7. #7
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringTokenizer phone number program

    PS: I AM trying. Made a change as you suggested, and am still getting 2 errors - just 1 of them is different. I'm still working... just obviously am missing something & don't understand.

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

    Default Re: StringTokenizer phone number program

    Quote Originally Posted by Erykah View Post
    yet still couldn't figure it out.
    That provides us with zero information. We cannot read your screen or your mind so we have no idea what you have tried.
    I don't understand because it's saying it "cannot find symbol" for return exchange.nextToken & return extension.nextToken
    I alread explained the String class does not have a nextToken method. You cannot just call any method and expect that the compiler will know which class you are talking about. Just because you use a StringTokenizer elsewhere in the class it cannot automagically go "Oh the nextToken belongs to the StringTokenzer class and not the String class. I call that method instead"
    I'm also unsure why the getExtension needs to be changed if it isn't returning an error.
    Just because something compiles without errors does not mean it is correct. Just because you make a change and it produces more errors does not mean that the change was wrong. What you have to do is then correct those new errors as well.
    I'm just a student looking for help - my teacher snaps when she is asked, so I was hoping for a better response here. :-(
    I don't respond well to students who blame their teachers.

  9. #9
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringTokenizer phone number program

    Thank you. I really am a beginner - I thought that once the utilities were called at the beginning, every one of those methods would be available throughout. It was my understanding that if I just mentioned the StringTokenizer prior to calling nextToken, all would be well. It's frustrating not to know these things & not understand why I'm getting errors. I was not blaming my teacher, but I have seen people get very friendly & helpful assistance here, which is more than I've learned to expect in class. Since this isn't covered in my text, I am trying to figure it out based on what I can find online, and the help you've provided.

    I didn't know that more errors doesn't necessarily mean I was moving in the wrong direction. I've always assumed that anything which increases the amount of errors was the wrong thing to do. Now, I have 6 errors, all stemming from the getter code. From "public String getAreaCode () {" the errors are "illegal start to expression" & "; expected" before the parenthesis.

    I'll keep trying & see if I can minimize those errors... Thank you

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

    Default Re: StringTokenizer phone number program

    Methods are available throughout your code. However the method MUST belong to the class of the Object you are calling it on.
    Java Code:
    String text = "hello";
    ArrayList list = new ArrayList();
    System.out.println(list.charAt(0));
    In the above code I have created 2 objects: a String and an ArrayList. The charAt method is a method of the String class and not the ArrayList class. The out put WILL NOT be 'h' because that is the character at position 0 of the string "hello". You cannot just call any method on any object and expect it to work. The ArrayList class DOES NOT have the charAt method so you cannot call it on an object of that class. I don't know how to make it any clearer than that. If you do not understand then you need to read your textbook/lecture notes and keep reading them until you do understand.

    If you have any problems with your code then you need to post your code and use tags to retain formatting. Place [ code ] before and [ /code ] after your code (without the spaces). You should also copy and paste the EXACT error messages you get. As I said we don't read minds. Saying "it doesn't work" doesn't help us at all.

  11. #11
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringTokenizer phone number program

    I think I'm understanding you: based on what you said, I think line 3 could be changed to "System.out.println(text.charAt(0));" to make it work? I will definitely reread the book, but part of my project for Thanksgiving break is to locate a java book that works better for me. I don't have much luck with the way info is presented in our text & would like another resource on hand so that I feel more self-sufficient.

    This is where I'm at now. I've been looking at it so much that it's not making as much sense - probably time to try a different assignment & come back after I clear my head.

    Java Code:
    // get utilities to use
    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.lang.Object;
    import java.lang.String;
    import java.io.*;
    
    public class ELShomework4a {
    static String exchange, extension;
    
    	public static void main(String[] args) {
    		// declare phone number string
    		String phoneNum = "(123) 456-7890";
    					
    		// get & print numbers from methods
    		String areaCode = getAreaCode();
    		System.out.print(areaCode);
    			
    		String exchange = getExchange(phoneNum);
    		System.out.print(exchange);
    		
    		String extension = getExtension(phoneNum);
    		System.out.print(extension);
    		
    		// user input phone number
    		//System.out.println( "Please enter a phone number in format \"(123) 456-7890\"." );
    		//System.out.flush();
    		//phoneNum = String.readLine();
    			
    		// check number format & ask for new input if incorrect		
    		//if(phoneNum == null||phoneNum.length() !=14)
    		//{
    			//System.out.println( "Please enter a phone number in format \"(123) 456-7890\"." );
    			//System.out.flush();
    		//}
    	}	
    	// separate each section of phone #
    	public String separateTokens(String phoneNum) {
    		// set delimiters, & exclude delimiter char from return		
    		StringTokenizer areaCode = new StringTokenizer(phoneNum, "()- "); {			
    			// reports just the #s between parenthesis			
    			areaCode.nextToken();
    		StringTokenizer exchange = StringTokenizer(phoneNum, "()- "); {
    			exchange.nextToken();
    		StringTokenizer extension = StringTokenizer(phoneNum, "()- "); {
    				extension.nextToken();			
    		}
    	}
    	public String getAreaCode() {
    		return areaCode;
    	}
    	public String getExchange()	{
    		return exchange;
    	}			
    	public String getExtension() {
    		return extension;
    	}
    }

    current errors
    ELShomework4a.java:54: illegal start of expression
    public String getAreaCode() {
    ^
    ELShomework4a.java:54: ';' expected
    public String getAreaCode() {
    ^
    ELShomework4a.java:57: illegal start of expression
    public String getExchange() {
    ^
    ELShomework4a.java:57: ';' expected
    public String getExchange() {
    ^
    ELShomework4a.java:60: illegal start of expression
    public String getExtension() {
    ^
    ELShomework4a.java:60: ';' expected
    public String getExtension() {
    ^
    ELShomework4a.java:63: reached end of file while parsing
    } →
    ^
    7 errors

    Thank you for taking time to explain. That's a much better example than I've seen elsewhere & hopefully I applied the knowledge better.

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

    Default Re: StringTokenizer phone number program

    OK now I can see your confusion. You have 2 variables called extension. One is a String and one is a StringTokenizer. Due to scope (you probably need to revise this as well) where you call extension.nextToken it does not have access to the Stringtokenizer version but it does have access to the String version. This is why you had your initial problems.

    Now your problems are in the separateTokens method. Why are you creating more than one StringTokenizer? You only need one to break the String into the separate parts. Then get the tokens out of the StringTokenizer and assign to the correct variables (areacode, exchange and extension). Due to the excessive and unnecessary use of the brackets { } in that method you have confused the compiler. The brackets are either in the wrong place (why do you have them at all) or they do not match.

  13. #13
    Erykah is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringTokenizer phone number program

    That's very helpful - thank you! I've been through way too many edits at this point, and seem to have made it worse since I didn't really understand the big picture of what I was trying to correct. A lot of those brackets are leftovers from that process.

    I've cleaned up the code now, and will restart with a clear head tomorrow, focusing on the duplicate variables you mentioned first. I didn't realize they weren't the same thing, so that should clear up a lot, and I'll learn more by sitting down to figure out what I need to do. Thank you very much!

Similar Threads

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

Tags for this Thread

Posting Permissions

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