• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

formatting phone number using NumberFormat?

 
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to format a phone number using NumberFormat class? If not, how is it done?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to ask questions in the appropriate forum. While I'm sure that you are eventually planning to use your formatted phone number in a JSP, the question is clearly a general Java issue and not one with JSP technology.

Moving to the Java in General(intermediate) forum.
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that I know of. If you're reading the phone numbers as a numeric type, you can always override the toString() method of your class to format and return the phone number in a user-friendly manner.
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can do it completely with NumberFormat. I've played with this sort of thing, and got part way like this:


This prints "Some mobile number:413 496 888".

There are a few problems with this, the first being that I wanted that number to begin with a zero (it should begin "0413" - all Aussie mobile numbers begin "04"). How can you account for that? Ultimately I think phone numbers should be represented as a String, not a number. The problem of formatting them easily remains, though

Cheers,



--Tim
[ May 27, 2004: Message edited by: Tim West ]
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Not that I know of. If you're reading the phone numbers as a numeric type, you can always override the toString() method of your class to format and return the phone number in a user-friendly manner.



Ok, but how do I format the String?
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please inform me, if you have a clever solution.

Of course there are some musts:
My international number is +49-30-789 50 798 (last three digits are fake, for keeping it private).
The first two indicate germany - from most countries you have to call 0049, from some 049 and from others it's simply false
If you're calling from germany, you may omit the +49, but need a 0 before '30' for Berlin, which you don't need from foreign countries.
From Berlin, you may omit the 030.

Smaller cities have more digits: Trier: 0651, Elstal: 033 234.

The mobile-phone is 0176-676 43 49. (last digits fake, to prevent Ranchers from calling me, early in the morning...)

From foreign countries, I guess, it's the same pattern, with provider instead of city: +49-176-676 43 49.

But now it's getting interesting:
While there is a industrial norm in Germany (DIN, like ISO in the US) for formatting telephonenumbers, which tells to split it in groups of 3 digits from the end:
78 950 798
_6 763 349
I don't recognize my own number in the above formats.

You may often detect a pattern in a number, which is easy to memorize.
My homenumber starts with 789 - a sequence.
And the 798 in the end is a variation in sorting of the first three digits.
So 789 50 798 is my prefered grouping for that number.

676 is a pattern too.
676 33 49 is more easy to learn than
6 763 349.

Think of
49 49 38 38 or
49 493 838.

Write a clever pattern-recognition program an make it OpenSource!

And provide an interface for dull dialing machines, which only get the plain number, unformatted: 00493078950798

Last but not least: Don't call us, but we call you...
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's surely possible to write a parser to do what you want. But I wonder if it's possible to use Locale class to distinguish between different cities, because doesn't Locale works only for countries?
The problem still remains, how do you parse a String using format pattern in lines of (###)###-#### or #-###-###-#### or ###-###-#### etc...
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently JDK 1.4 introduced new Class: MaskFormatter. This allows you to format your text how you want it. Unfortunately, we are using 1.3 so...out of luck.
http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
reply
    Bookmark Topic Watch Topic
  • New Topic