| | |
StringTokenizer
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows





