| | |
StringTokenizer
![]() |
•
•
Join Date: Oct 2004
Posts: 2
Reputation:
Solved Threads: 0
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");
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!
Ed
Java Syntax (Toggle Plain Text)
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
•
•
•
•
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.
![]() |
Similar Threads
- array from StringTokenizer (Java)
- help on using StringTokenizer to read and compute multiple lines from text (Java)
- StringTokenizer: To searh and replace a word. (Java)
- StringTokenizer??? NEED HELP PLEASE!!! (Java)
- StringTokenizer (Java)
Other Threads in the Java Forum
- Previous Thread: URGENT: Need help on I/O code!
- Next Thread: Saving and opeing a JTable
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





