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 19 of 19

Thread: phone number generator

  1. #1
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default phone number generator

    import java.util.Random;
    public class RandomPhoneNumber
    {
    public static void main (String[]args)
    {Random generator = new Random();


    /*write down the RandomNumber AreaCode1 AreaCode2 AreaCode3;
    *choose the first 3
    *choose the second 3
    *choose the last four
    *combine them
    */
    int RandomNumber =0;
    int Areacode;
    int exchange;
    int LastFour =0;
    int thenumber = 1212;

    System.out.println(" Enter the amount of numbers you want to receive: ");
    RandomNumber = generator.nextInt();
    Areacode = generator.nextInt();
    if ( Areacode = 281 || int Areacode = 713 || int Areacode =832)
    { }

    exchange =( int )(237 + Math.random() * 517);
    while (LastFour != 1212)
    LastFour = (int) (0 + Math.random() *9999);
    String string1 = Integer.toString(thenumber);

    System.out.println( "Phone numbers that resulted" + Areacode + "-" exchange + "-" LastFour);
    System.out.println();

    }
    }






    please help im desperate I need to make it work but don't know how!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    Please explain what the problem is. Post the program's output and add some comments say what is wrong.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    Create a new java file named RandomPhoneNumber.java

    Using Math.random(), write a program that creates and outputs random phone numbers of the form (xxx) xxx-xxxx. Include the parentheses and dashes in the output. The first three digits should randomly be 281, 713, 832. The second three should be between 237 and 754. The last four should range between 0 and 9999, excluding 1212. You will input from the keyboard the amount of phone numbers to generate.

    Note: each digit does not have to be determined separately.


    that is what I have to do and I don't know what else to do.. im completely lost

    also thank you for telling me how it works

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    Have you done any experimenting with the Math.random() method to see what it does?
    Write a small simple test program with a loop that calls Math.random() a few times and prints out what it returns to see.

    For your project, break it up into separate steps and work on them one by one.
    First: first three digits should randomly be 281, 713, 832
    Put those 3 sets of digits in an array (length = 3) and generate a random number from 0 to 2 to randomly select one of the three.
    When that step works, pick the next step and work on it.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    /*juliana Vargas
    programing
    10/29/2013
    */
    import java.util.Random;
    public class RandomPhoneNumber
    {
    public static void main (String[]args)
    {Random generator = new Random();


    /*write down the RandomNumber AreaCode1 AreaCode2 AreaCode3;
    *choose the first 3
    *choose the second 3
    *choose the last four
    *combine them
    */
    int RandomNumber =0;
    int exchange;
    int LastFour =0;


    System.out.println(" Enter the amount of numbers you want to receive: ");


    String [] Areacode = {"713", "281", "832"};
    System.out.format("The Java array length is %d", Areacode.length);
    RandomNumber = generator.nextInt();
    exchange =( int )(237 + Math.random() * 517);
    while (LastFour != 1212)
    LastFour = (int) (0 + Math.random() *9999);
    System.out.println( "Phone numbers that resulted" +Areacode);


    }
    }


    this is what I have now the thing is that my jcreator doesn't work it won't run ... but at least it shows me that is right... I have tried everything but still won't work

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    sorry didn't see it

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    still won't work
    Please explain.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    yuli (November 3rd, 2013)

  10. #9
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    I can't tell because my jcreator doesn't run... I don't know why... thank you very much for your help... I will work on it more tomorrow if it works I would be please to thank you.. have a great night.

  11. #10
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    this is the new one I made but at the end it wont give me the last four numbers and I cant input the amount of phone numbers

    --- Update ---

    /*juliana Vargas
    programing
    10/29/2013
    */
    import java.util.Random;
    public class RandomPhoneNumber
    {
    public static void main (String[]args)
    {Random generator = new Random();


    /*write down the RandomNumber AreaCode1 AreaCode2 AreaCode3;
    *choose the first 3
    *choose the second 3
    *choose the last four
    *combine them
    */

    int RandomNumber =0;
    int exchange;
    int LastFour =0;
    int number;
    int num1 = 0;

    System.out.println(" Enter the amount of numbers you want to receive: ");
    number = generator.nextInt();

    num1 = (int)(1+Math.random()*3);
    if (num1 == 1)
    num1 = 281;
    System.out.printf(""+num1);
    if (num1 == 2)
    num1 = 713;
    System.out.printf(""+num1);
    if (num1 == 3)
    num1 = 832;
    System.out.printf(""+num1);

    RandomNumber = generator.nextInt();

    exchange =( int )(237 + Math.random() * 517);
    RandomNumber = generator.nextInt();

    while (LastFour == 1212)
    LastFour = (int) (0 + Math.random() * 9999);
    RandomNumber = generator.nextInt();

    System.out.println( "Phone numbers that resulted " +"("+num1+")" +exchange +"-"+LastFour);


    }
    }

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    how what you mean sorry?.. how?

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    Look at some of the other posts on the forum to see how code is posted.

    Edit your post and put the following line just before your code:
    [code=java]

    Put the following line just after your code:
    [/code]
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    /*juliana Vargas
    programing
    10/29/2013
    */
    import java.util.Random;
    public class RandomPhoneNumber
    { 
     public static void main (String[]args)                  
     {Random generator = new Random();
     
     
      /*write down the RandomNumber AreaCode1 AreaCode2 AreaCode3;
       *choose the first 3
       *choose the second 3
       *choose the last four
       *combine them 
       */
     
      int RandomNumber =0;
      int exchange;
      int LastFour =0;
      int number;
      int num1 = 0;
     
        System.out.println(" Enter the amount of numbers you want to receive: ");
        number = generator.nextInt();
     
        num1 = (int)(1+Math.random()*3);
        if (num1 == 1)
         num1 = 281;
         System.out.printf(""+num1);
        if (num1 == 2)
         num1 = 713;
         System.out.printf(""+num1); 
        if (num1 == 3)
         num1 = 832; 
         System.out.printf(""+num1);  
     
        RandomNumber = generator.nextInt();
     
        exchange =( int )(237 + Math.random() * 517);
         RandomNumber = generator.nextInt();
     
        while (LastFour == 1212)
        LastFour = (int) (0 + Math.random() * 9999); 
        RandomNumber = generator.nextInt(); 
     
        System.out.println( "Phone numbers that resulted " +"("+num1+")" +exchange +"-"+LastFour);
     
     
     }
    }

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    Yes, that looks much better.

    Can you copy the contents of the command prompt window and paste it here that shows the problem that you are talking about? Add some comments to the post showing what is wrong and what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    ohhh I see what you meant hopefully now its better

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    One potential problem I see is that the code following if statements is not enclosed in {}s. Leaving off the {}s makes the code harder to read and harder to change.
    ALWAYS add {}s following if and loops
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: phone number generator

    /*juliana Vargas
     programing
     10/29/2013
     */
     import java.util.Random;
     public class RandomPhoneNumber
     { 
     public static void main (String[]args) 
     {Random generator = new Random();
     
     
     /*write down the RandomNumber AreaCode1 AreaCode2 AreaCode3;
     *choose the first 3
     *choose the second 3
     *choose the last four
     *combine them 
     */
     
     int RandomNumber =0;
     int exchange;
     int LastFour =0;
     int number;
     int num1 = 0;
     
    // command to input the amount of numbers I want...( it wont let me input anything)
     System.out.println(" Enter the amount of numbers you want to receive: ");
     number = generator.nextInt();
     
    //use random to find the first 3 numbers
     num1 = (int)(1+Math.random()*3);
     if (num1 == 1)
     num1 = 281;
     System.out.printf(""+num1);
     if (num1 == 2)
     num1 = 713;
     System.out.printf(""+num1); 
     if (num1 == 3)
     num1 = 832; 
     System.out.printf(""+num1); 
     
     RandomNumber = generator.nextInt();
     
    // use random to find the second 3 numbers
     exchange =( int )(237 + Math.random() * 517);
     RandomNumber = generator.nextInt();
     
    //find the las 4 numbers except for 1212 (it will only give me  a zero)
     while (LastFour == 1212)
     LastFour = (int) (0 + Math.random() * 9999); 
     RandomNumber = generator.nextInt(); 
     
     System.out.println( "Phone numbers that resulted " +"("+num1+")" +exchange +"-"+LastFour);
     
     
     }
     }


    --- Update ---

    like this {num1 = 281;} ?

    --- Update ---

    hmm.. also for the while loop?

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: phone number generator

    No, what you posted was not complete.
    Copy the WHOLE contents of the command prompt window.
    It should include this:
    Enter the amount of numbers you want to receive:
    and what was typed in by the user
    and what was printed by the program.


    The code posted in post#18 has lost its formatting and does not have {}s for the if and while statements as I suggested.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Random Number Generator
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 12:58 AM
  2. Mobile Number Generator
    By djl1990 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 28th, 2012, 07:44 PM
  3. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  4. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM