Results 1 to 10 of 10
  1. #1
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default 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. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    // 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.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    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. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    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

  5. #5
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    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. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    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

  7. #7
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    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. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    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

  9. #9
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    Okay ..
    thanks a lot, dude ..
    I really appreciate ur help ..

    Regards,
    Sary

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Post again, if you get stuck.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 AM
  2. Replies: 5
    Last Post: 11-02-2008, 07:58 PM
  3. Is it possible to make a Phone call program using java?
    By fireball2008 in forum New To Java
    Replies: 2
    Last Post: 05-08-2008, 06:20 PM
  4. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  5. Menu on phone number option of a list
    By Poonam in forum CLDC and MIDP
    Replies: 7
    Last Post: 01-31-2008, 01:42 PM

Posting Permissions

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