2 Replies - 1313 Views - Last Post: 11 May 2011 - 12:16 PM Rate Topic: -----

#1 Clewless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 11

Tokenizing Telephone Numbers

Posted 11 May 2011 - 11:03 AM

I'm looking for some assistance, this is the code I have so far.
I'm supposed to take a single input. EX: (555) 555-5555 and have it then use the tokenized strings and split it up into three separate categories. Area code, first three digits and last four digits. Also the seven digits of the phone number must be concatenated into one string. I'm lost any help would be amazing thanks.

package p16_8;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
 *
 * @author ZP-Threat
 */
public class Main {
int areacode;
int first3;
int last4;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Get the number
        Scanner scanner = new Scanner( System.in );
        System.out.println( "Enter a telephone number and press Enter");
        String sentence = scanner.nextLine();
        //Process the number and split it up
        String[] tokens = sentence.split( " " );
        System.out.printf( "The area code is: %d\n", tokens.length );
    for ( String token : tokens )
        System.out.println( token );
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Tokenizing Telephone Numbers

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Tokenizing Telephone Numbers

Posted 11 May 2011 - 12:04 PM

	String num = "(555) 555-5555";
	String[] tokens = num.replaceAll("[()]", "").split("[ -]");
	String joined = num.replaceAll("\\D", "");
	System.out.println(Arrays.toString(tokens));
	System.out.println(joined);


Was This Post Helpful? 2
  • +
  • -

#3 Clewless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 11

Re: Tokenizing Telephone Numbers

Posted 11 May 2011 - 12:16 PM

Imported the array and it works like a charm! I just have to modify it to accept whatever number I input! Thanks man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1