Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Program of creating and printing a random phone number that takes only 8-9 in first three digits

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Program of creating and printing a random phone number that takes only 8-9 in first three digits

    I am going through a Lewis and Loftus book doing all of the examples, trying to get myself reacquainted with the Java language. I've included the exercise's requirements inside of the [CODE] tags.

    I've tried everything from a BUNCH of if statements, to trying to make up the 'area' variable out of three separate characters, but I cannot seem to get it right. How do I go about limiting the output so that there are no 8 or 9s?

    import java.util.Random;
     
    /* 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 that), and make sure that the second set of three digits is
    not greater than 742.*/
     
    class Test {
     
      public static void phoneNumber(){
        Random random = new Random();
     
        int area = random.nextInt(900)+100;
        int mid = random.nextInt(643)+100;
        int last = random.nextInt(9000)+1000;
     
        System.out.println(area+"-"+mid+"-"+last);
      }
     
      public static void main(String[] args){
        phoneNumber();
     
       }
      }
    Last edited by Deep_4; November 8th, 2012 at 02:14 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Formatting Output

    I used strings to hold the values, since number data types don't keep track of the zeros very well.
        public static void main (String[] args)
        {
            Random gen = new Random();
            // first 3 digits. Can't contain an 8 or 9
            String areaCode = "";
            for (int i = 0; i < 3; i++)
            {
                areaCode += gen.nextInt(8);
            }
            // second three digits must be less than or equal to 742
            String second = "" + gen.nextInt(743);
            while (second.length() < 3)
            {
                second = "0" + second;
            }
            // last 4 can be anything
            String last = "" + gen.nextInt(10000);
            while (last.length() < 4)
            {
                last = "0" + last;
            }
            System.out.printf(areaCode + "-" + second + "-" + last);
        }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    cfmonster (August 19th, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Formatting Output

    Yeah I agree with helloworld, after the experience I got from the telecom industry I noticed that integers is the worst way of representing phone numbers.

    // Json

  5. #4
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Formatting Output

    Thanks. Slowly but surely getting the hang of Java again

Similar Threads

  1. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM
  2. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 09:33 AM
  3. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM