943,936 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7866
  • Java RSS
Oct 5th, 2004
0

StringTokenizer

Expand Post »
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");
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunsol is offline Offline
2 posts
since Oct 2004
Oct 6th, 2004
0

Re: StringTokenizer

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

Java Syntax (Toggle Plain Text)
  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


Quote 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");
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Oct 6th, 2004
0

Re: StringTokenizer

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunsol is offline Offline
2 posts
since Oct 2004
Oct 12th, 2004
0

Re: StringTokenizer

Quote 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.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: URGENT: Need help on I/O code!
Next Thread in Java Forum Timeline: Saving and opeing a JTable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC