I need some help!! I need to have a user input a phone number and then spit it out using StringTokenizer. This is what I have so far:

import java.util.*;
import java.io.*;
class Prob8_2
{
    public static void main (String []args)throws IOException
    {
        BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

        String phonen = null;
        String token = null;

        do
        {
                         //user enters phone number
            System.out.print( "Enter phone number: " );
            System.out.flush();
            phonen = stdin.readLine();
                         //some error checking
            if ( phonen == null || phonen.length() != 12)
            {
                System.out.println( "Please enter phone number in this format \"555-555-5555\"." );
                System.out.flush();
            }
        }

/ This is where I'm lost....It has to be printed out in the similar to the
// following:
// 555 is the area code
// 555 is the exchange
// 5555 is the extension
        while (phonen == null || phonen.length() != 12 );
        {
            tokenizer = new StringTokenizer(phonen, "-");
        }
        System.out.print(getAreaCode(tokenizer) + " is the area code");
        System.out.print(getExchange(tokenizer) + " is the exchange");
        System.out.print(getExtension(tokenizer) + " is the extension");

Check the Java Almanac entry for StringTokenizer---it is very useful!

String aString = "555-555-55555";
  StringTokenizer parser = new StringTokenizer(aString, '-');

  if (aString.countTokens() != 3) {
     // error input is invalid
  }  

  String areaCode = parser.nextToken();  // area code
  String exchange = parser.nextToken();
  String extension = parser.nextToken();

Ed

I need some help!! I need to have a user input a phone number and then spit it out using StringTokenizer. This is what I have so far:

import java.util.*;
import java.io.*;
class Prob8_2
{
public static void main (String []args)throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

String phonen = null;
String token = null;

do
{
//user enters phone number
System.out.print( "Enter phone number: " );
System.out.flush();
phonen = stdin.readLine();
//some error checking
if ( phonen == null || phonen.length() != 12)
{
System.out.println( "Please enter phone number in this format \"555-555-5555\"." );
System.out.flush();
}
}

// This is where I'm lost....It has to be printed out in the similar to the
// following:
// 555 is the area code
// 555 is the exchange
// 5555 is the extension
while (phonen == null || phonen.length() != 12 );
{
tokenizer = new StringTokenizer(phonen, "-");
}
System.out.print(getAreaCode(tokenizer) + " is the area code");
System.out.print(getExchange(tokenizer) + " is the exchange");
System.out.print(getExtension(tokenizer) + " is the extension");

Thanks for the help. I just learned about java.sun the other day. I'm a newbie so its still greek to me. :~)

Thanks for the help!!

sunsol

Thanks for the help. I just learned about java.sun the other day. I'm a newbie so its still greek to me. :~)

Thanks for the help!!

sunsol

I heard recently that StringTokenizer was deprecated in 1.5. An alternative method is suggested in the String class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.