StringTokenizer

Reply

Join Date: Oct 2004
Posts: 2
Reputation: sunsol is an unknown quantity at this point 
Solved Threads: 0
sunsol sunsol is offline Offline
Newbie Poster

StringTokenizer

 
0
  #1
Oct 5th, 2004
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");
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: StringTokenizer

 
0
  #2
Oct 6th, 2004
Check the Java Almanac entry for StringTokenizer---it is very useful!

  1. String aString = "555-555-55555";
  2. StringTokenizer parser = new StringTokenizer(aString, '-');
  3.  
  4. if (aString.countTokens() != 3) {
  5. // error input is invalid
  6. }
  7.  
  8. String areaCode = parser.nextToken(); // area code
  9. String exchange = parser.nextToken();
  10. String extension = parser.nextToken();


Ed


Originally Posted by sunsol
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");
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 2
Reputation: sunsol is an unknown quantity at this point 
Solved Threads: 0
sunsol sunsol is offline Offline
Newbie Poster

Re: StringTokenizer

 
0
  #3
Oct 6th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: StringTokenizer

 
0
  #4
Oct 12th, 2004
Originally Posted by 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC