# Phone number Program ..

• 03-17-2010, 06:02 PM
Sary
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: -

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);     }     }```
• 03-17-2010, 07:23 PM
PhHein
Quote:

// 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.
• 03-17-2010, 07:27 PM
Sary
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:
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 !
• 03-17-2010, 07:33 PM
PhHein
How would a radix of 8 help you if the user enters a 8 or 10?
• 03-17-2010, 07:36 PM
Sary
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 ();
• 03-17-2010, 07:41 PM
PhHein
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?
• 03-17-2010, 07:44 PM
Sary
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 ..
• 03-17-2010, 07:48 PM
PhHein
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.
• 03-17-2010, 07:50 PM
Sary
Okay ..
thanks a lot, dude ..
I really appreciate ur help ..

Regards,
Sary
• 03-17-2010, 08:15 PM
PhHein
Post again, if you get stuck.