programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# PhoneNumbers .java

Lj Yanuaria
Greenhorn
Posts: 1

Instruction:

You work for a telemarketing company and you are required to write a JAVA program that will generate a random phone number. (talk about a real-world application)

The phone number should consist of 10 digits
The first 3 are the area code and should not begin with 0, 8 or 9
The second 3 digits should not be greater than 742 and not less than 100.
The last 4 digits can be any digits
Print the number using the following format: "(xxx)-xxx-xxxx", this way it will look like a real phone number (use decimal formatting)

by this : Generating Random Numbers
Using arithmetic operations
Develop logic skills

I have been storing phone numbers as longs and I would like to simply add hyphens when printing the phone number as a string.

I tried using DecimalFormat but that doesn't like the hyphen. Probably because it is meant for formatting decimal numbers and not longs.

long phoneFmt = 123456789L;
DecimalFormat phoneFmt = new DecimalFormat("###-###-####");
System.out.println(phoneFmt.format(phoneNum)); //doesn't work as I had hoped

Ideally, I would like to have parenthesis on the area code too.

new DecimalFormat("(###)-###-####");

What is the correct way to do this?

Joanne Neal
Rancher
Posts: 3742
16
You probably need to read this

Henry Wong
author
Sheriff
Posts: 23295
125
Lj Yanuaria wrote:
I have been storing phone numbers as longs and I would like to simply add hyphens when printing the phone number as a string.

I tried using DecimalFormat but that doesn't like the hyphen. Probably because it is meant for formatting decimal numbers and not longs.

long phoneFmt = 123456789L;
DecimalFormat phoneFmt = new DecimalFormat("###-###-####");
System.out.println(phoneFmt.format(phoneNum)); //doesn't work as I had hoped

Ideally, I would like to have parenthesis on the area code too.

new DecimalFormat("(###)-###-####");

What is the correct way to do this?

Two possibilities. Option one, convert the long to a string, break it into the three components -- and then build an overall string using the three strings components, and also adds the parens and minus sign. Keep it as a long, and use a combination of the divide and modulus operator to generate the three components as three ints -- and then generate a string using the three ints (again adding the parens and minus).

Henry

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Lj Yanuaria wrote:
What is the correct way to do this?

I would make several suggestions...

A phone number isn't a single number. It's three numeric parts - the area code, the exchange, and the...i don't know what it is called. But trying to store all three parts as a single number just seems wrong to me.

I would suggest you break the problem into multiple parts. For example, how you print it should be independent of how you generate it. Generating the area code has its own set of rules, and should be independent from generating the exchange.

Steve Fahlbusch
Bartender
Posts: 612
7
Wow it has been years since i saw someone use exchange :-)

Usually the 3-3-4 parts are called area code, prefix and line (or line number).

But as Fred said - the prefix can also be called exchange. And truth be told, i kind of like exchange over prefix.

Robert D. Smith
Ranch Hand
Posts: 221
5
My first thoughts are similar to what Fred has already posted.

The problem description has three distinct parts, and each part has different rules; ergo -- create three holders, one for each value. *Then* you can worry about how to put them together.
You are trying to get to the end of the program before you have begun. Turn off the computer, write out all the steps on paper. I leave the choice of pencil vs. pen to you.

Campbell Ritchie
Marshal
Posts: 56529
172
Pencil. I say, always use a pencil. The softer the better, because you will need to erase it frequently

Winston Gutkowski
Bartender
Posts: 10575
66
• 1
Steve Fahlbusch wrote:And truth be told, i kind of like exchange over prefix.

Me too; and given that the style looks like the North American system, I'm surprised they didn't disallow '555' as well.

@Li: All the good advice above is predicated on one thing: writing classes, not code.

Java is an object-oriented language, so whenever you're writing a program you should be thinking: What classes is it going to need?
And this will usually come from the requirements:
Need a customer? Write a Customer class.
Need a temperature gauge? Write a Thermometer class.
Need a phone number? Write a PhoneNumber class.

You already have a PhoneNumbers class, but you're simply using it as a place to dump ALL your code. Don't. Most Java programs will involve several classes, so if you find yourself with only one, you should feel uneasy.

The great thing about doing that is that you can then decide what your PhoneNumber is going to look like internally (long, Strings, 3 separate fields etc.) - and even change it if it doesn't work. And if you write it well, those changes shouldn't have any effect on the rest of your program.

HIH

Winston

PS: Welcome to JavaRanch, Li.

 It is sorta covered in the JavaRanch Style Guide.