# Thread: Phone number Program ..

1. Member
Join Date
Mar 2010
Posts
15
Rep Power
0

## Phone number Program ..

Hello All ..

Write an application that creates and prints a random phone number of the form xxx-xxx-xxxx. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 (but don't be more restrictive than than), and make sure that the second set of three digits is not greater than 742. Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately.

This is the question .. I've done 90% of the program .. but I wanna ask about something .. if the question stated that "each digit does not have to be determined separtely" .. then how can i shall not allow the first 3 digits contain an 8 or 9 ?!

I've done this: -

Java Code:
```package phonenumber;

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner scan = new Scanner (System.in);
Random r = new Random ();
int abc,d,efgh;

abc = r.nextInt(1000);
// What shall I do here in order not to let abc contain an 8 or 9 ?

efgh = r.nextInt(10000);

d = r.nextInt(743);
if ( d >= 100 && d <= 742)
System.out.println ("The phone number is: " + abc + "-" + d + "-" + efgh);
if ( d < 100 && d >= 10)
System.out.println ("The phone number is: " + abc + "-" + "0" + d + "-" + efgh);
if ( d < 10 && d > 0)
System.out.println ("The phone number is: " + abc + "-" + "00" + d + "-" + efgh);
if ( d == 0)
System.out.println ("The phone number is: " + abc + "-" + "000" + "-" + efgh);
}

}```

2. // What shall I do here in order not to let abc contain an 8 or 9 ?
You could convert the int to a String and use String.indexOf() or String.matches() to find 8s and 9s . Loop until you've found a number that is OK.

3. Member
Join Date
Mar 2010
Posts
15
Rep Power
0
is there any other way instead of using indexOf of .matches ?
coz I dunno about them yet ..
if there isn't .. is it ok to say:
Java Code:
```int a,b,c;
a = scan.nextInt (8);
b = scan.nextInt (8);
c = scan.netInt (8);```
coz what i've didn't take in the course yet, isn't included in my assignment !

4. How would a radix of 8 help you if the user enters a 8 or 10?

5. Member
Join Date
Mar 2010
Posts
15
Rep Power
0
ohhh sorry, mate ..
i didn't mean to say scan.nextInt(8);
really sorry ..
I meant to say r.nextInt(8);
assuming that r is an object from the random class ..
as to say: Randoum r = new Random ();

6. And I meant to say 7 or 10 ;)
You could do that three times to get the first three digits, but it looks clumsy. I don't know what you've learned in you course yet, so it's hard to give you an advice. But limiting yourself to stuff you have learned in the course is silly unless the course runs for the next 10 years :)
Would 007 be a valid entry for the first part?

7. Member
Join Date
Mar 2010
Posts
15
Rep Power
0
yes sure 007 will be valid .. since our limit is 8 .. so any number from 0 to 7 will be accepted ..
I think the way I said is true .. but what worries me is that the question says "each digit does not have to be determined separately" .. and in my way, I am determing the first 3 digits separately ..

8. Well, you don't have to if you use indexOf or matches. Read the String API to find out how to use them. If you don't use them you'll have to check every digit(at least of the first part).
But maybe I don't see the obvious here.

9. Member
Join Date
Mar 2010
Posts
15
Rep Power
0
Okay ..
thanks a lot, dude ..
I really appreciate ur help ..

Regards,
Sary

10. Post again, if you get stuck.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•