How to format phone numbers

How can i format phone number in java programming without using substrings
[81 byte] By [sammu9a] at [2007-10-2 1:12:42]
# 1
For which country?
stdunbara at 2007-7-15 18:33:28 > top of Java-index,Java Essentials,Java Programming...
# 2
US
sammu9a at 2007-7-15 18:33:28 > top of Java-index,Java Essentials,Java Programming...
# 3
Why don't you want to use subStrings? If that's the case you might want to look at java.util.regex.
kefgolfsa at 2007-7-15 18:33:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Homework, right?

Convert the String to a char array with toCharArray() and pull out what you want - i.e.:

String wholePhoneNumber = "2125551212";

char[] numberAsAnArray = wholePhoneNumber.toCharArray();

char[] areaCodeArray = new char[3];

areaCodeArray[0] = numberAsAnArray[0];

areaCodeArray[1] = numberAsAnArray[1];

...

There are more efficient ways but this is likely to get the problem solved.

stdunbara at 2007-7-15 18:33:29 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks for the reply but can't i do using text formatter. the problem is when i am using substring i am getting array out of bound exception because substring is not holding the maximum number which i have given. the phone number is dynamic it can have 2 spaces or 4 spaces but format output should be xxx-xxx-xxxx

how about message formatting? how it works

sammu9a at 2007-7-15 18:33:29 > top of Java-index,Java Essentials,Java Programming...
# 6
See MessageFormat and Scanner
tjacobs01a at 2007-7-15 18:33:29 > top of Java-index,Java Essentials,Java Programming...
# 7
can you send code snippet
sammu9a at 2007-7-15 18:33:29 > top of Java-index,Java Essentials,Java Programming...
# 8
> can you send code snippetAll our spoons are in the dishwasher right now, sorry.
warnerjaa at 2007-7-15 18:33:29 > top of Java-index,Java Essentials,Java Programming...