Results 1 to 10 of 10
Thread: Phone number Program ..
- 03-17-2010, 05:02 PM #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); } }
- 03-17-2010, 06:23 PM #2
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.// What shall I do here in order not to let abc contain an 8 or 9 ?Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-17-2010, 06:27 PM #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:
coz what i've didn't take in the course yet, isn't included in my assignment !Java Code:int a,b,c; a = scan.nextInt (8); b = scan.nextInt (8); c = scan.netInt (8);
- 03-17-2010, 06:33 PM #4
How would a radix of 8 help you if the user enters a 8 or 10?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-17-2010, 06:36 PM #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 ();
- 03-17-2010, 06:41 PM #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?Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-17-2010, 06:44 PM #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 ..
- 03-17-2010, 06:48 PM #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.Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-17-2010, 06:50 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
Okay ..
thanks a lot, dude ..
I really appreciate ur help ..
Regards,
Sary
- 03-17-2010, 07:15 PM #10
Post again, if you get stuck.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
A Number Converting Program!
By WastedxYears in forum New To JavaReplies: 2Last Post: 01-09-2010, 12:47 AM -
Which program can i use to develop mobile phone programs?
By hewstone in forum New To JavaReplies: 5Last Post: 11-02-2008, 07:58 PM -
Is it possible to make a Phone call program using java?
By fireball2008 in forum New To JavaReplies: 2Last Post: 05-08-2008, 06:20 PM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
Menu on phone number option of a list
By Poonam in forum CLDC and MIDPReplies: 7Last Post: 01-31-2008, 01:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks