Results 1 to 2 of 2

Thread: How do I fix this error? (java)

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    How do I fix this error? (java)

    I get the error "(identifier) expected" for lines 26, 27, and 28.
    for example:
    areaCode = phone.nextToken();
    ^ (the arrow right after 'areaCode')



    Here is my code:


    import java.util.StringTokenizer;

    import java.util.Scanner;

    public class Phone {

    public static void main (String[] args) {

    Scanner scan = new Scanner(System.in);

    System.out.print ("Enter phone number in format (xxx) xxx-xxxx): ");
    StringTokenizer phone = new StringTokenizer (scan.nextLine()," ", "-");

    System.out.println ("Area Code: " + phone.get_areaCode());
    System.out.println ("Exchange: " + phone.get_exchange());
    System.out.println ("Extension: " + phone.get_extension());
    }
    }

    class PhoneTokenizer {

    private String areaCode;
    private String exchange;
    private String extension;

    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;

    }
    }

  2. #2
    Join Date
    Dec 2007
    Posts
    1,736

    Re: How do I fix this error? (java)

    Try to look at the code given below

    Code:
    import java.util.StringTokenizer;
    
    public class phone_Tokens {
    
    public static void main (String[] args) {
    
    String areacode, exchange, extension;
    
    // Creates tokens that are delimited by a dash, thus
    // (nnn) is one token, nnn is the next, and the last is nnnn
    StringTokenizer phone = new StringTokenizer ("(nnn)-nnn-nnnn", "-");
    
    areacode = phone.nextToken();
    exchange = phone.nextToken();
    extension = phone.nextToken();
    
    System.out.println ("Areacode: " + areacode);
    System.out.println ("Exchange: " + exchange);
    System.out.println ("Extension: " + extension);
    }
    }
    If you only need the three numbers inside the () for the area code, then see if you can figure out how to use an available String method to do so.

Similar Threads

  1. Getting error: "Java Error 1723” on my windows 7 system
    By Cajetan in forum Operating Systems
    Replies: 7
    Last Post: 26-01-2012, 02:39 AM
  2. Replies: 5
    Last Post: 15-12-2010, 07:18 PM
  3. Java ME SDK 3.0 Fatal Error : 123
    By Patricia-USA in forum Software Development
    Replies: 5
    Last Post: 17-07-2010, 03:32 AM
  4. Error message: Uncaught Exception java.lang.error
    By Rum in forum Portable Devices
    Replies: 5
    Last Post: 06-01-2010, 01:59 PM
  5. Java runtime error
    By pmela in forum Windows XP Support
    Replies: 2
    Last Post: 22-01-2008, 12:03 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,269,402.61893 seconds with 17 queries