Results 1 to 14 of 14
  1. #1
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Question Random Phone number generator

    Hi again guys, I've been working on a personal project again in Unit 3 of my text book and this time they want me to make a random phone number generator. Here's the actual directions:

    "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.
    Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately."

    With this assignment I came across 2 problems:

    1. What I tried was that since the first 3 numbers have to be a random number with no 8's or 9's I tried doing this algorithm:

    int first0, first1, first2, second, third, random0;

    first0 = generator.nextInt (999);
    random0 = generator.nextInt (8);
    first1 = first0.replace('8', random0);
    first2 = first1.replace('9' random0);

    The problem is that Java won't allow me to generate random numbers this way. Are there any easier ways of generating numbers that doesn't contain specific number (such as 8 or 9 in this case?)

    2. I was thinking about this throughout the whole project and what if I generate a number such as 27 (less than 3 or 4 digit number) rather than 3 or 4 digit numbers? How would it show 0027 or 027 instead of 27 for the phone numbers?

    P.S. oh by the way I am only in Unit 3 of the text book, which contains "creating objects", "The string class", "packages", "The random class", "The math class", "Formatting Output", and "Enumerated types". Anything beyond that is a problem for me, so if someone can help me with this within my knowledge, I will be greatful.

    MY ACTUAL WHOLE PROJECT:

    import java.util.Random;
    public class pp0303 {
    public static void main (String[] args)
    {
    Random generator = new Random();
    int first0, first1, first2, second, third, random0;

    first0 = generator.nextInt (999);
    random0 = generator.nextInt (8);
    first1 = first0.replace('8', random0);
    first2 = first1.replace('9' random0);

    second = generator.nextInt (743);

    third = generator.nextInt (9999);

    System.out.println ("Your new phone number is: " + first2 + " - " + second + " - " + third);

    }
    }


    RESULTS:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot invoke replace(char, int) on the primitive type int
    Cannot invoke replace(char) on the primitive type int
    Syntax error on token "random0", delete this token

    at pp0303.main(pp0303.java:15)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    first0.replace('8', random0);
    The variable first0 is a primitive which do not have methods.

    Two thoughts:

    Use a loop, generate a 3 digit random number, if it doesn't contain a 8 or a 9 exit loop.

    Generate 3 random numbers 0 inclusive to 8 exclusive and concatenate them together.

  3. #3
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    Thanks for you comment again Junky.
    However I can't seem to understand it... could you use easier language for this primitive user?
    I don't really know how to use loops which would exit if it contains 8 or 9.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You would need another nested loop. You can either convert the number to a String and check each char. Or you can use mod and divide by 10 to get each digit.
    Java Code:
    int number = 123;
    System.out.println(number % 10);
    System.out.println(number / 10);
    Above snippet shows how to do it for one digit. You can do that in a loop until you reach zero.

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Why don't you use a loop?
    Java Code:
    for (i <= 3)
    {
    random0 = generator.nextInt(8);
    myString = myString.concat(Integer.toString(random0));
    }
    Something like that would work. I didn't test my code segment but even if it doesn't work it should help you solve your problem.

  6. #6
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    How do you change an integer to String?
    I tried putting in the following:

    String first4;

    first4 = first0; // first0 was an integer.

    output was: "Type mismatch: cannot convert from integer to String"

    Also how can I use the above loop which you showed me effectively?
    I was pondering over the ways to use it but i couldn't come up with an answer.
    Would you mind showing me in more details?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Yep, that was my second suggestion. Probably a lot easier.

  8. #8
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    oh thanks guys~
    the reason why i didn't use loop was because the textbook still haven't gone through that far yet...
    but that was really helpful.
    I'll try using the above example and get back to you guys ASAP

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    My example is much simpler and will achieve the same results as what Junky was suggesting.

    The current final output of that snippet will result in the code being a string at the end of the loop. If you need the numbers to be integers then you can convert them back using this line.

    Java Code:
    int myInteger = Integer.parseInt(myString);

  10. #10
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    okay so i looked in the book and loops are in Unit 5, which means I am not suppose to use a loop... any suggestions without using loops?

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Do it the inefficient way.

    Java Code:
    random0 = generator.nextInt(8);
    myString = myString.concat(Integer.toString(random0));
    random0 = generator.nextInt(8);
    myString = myString.concat(Integer.toString(random0));
    random0 = generator.nextInt(8);
    myString = myString.concat(Integer.toString(random0));
    Loops are designed to increase the efficiency of repeated tasks. In this case 3 similiar tasks aren't a big deal, but if you had to run the same segment of code 10,000 times you would be prone to errors, and it would be a pain in the ass to code it all. Even copy paste would be tedious.

  12. #12
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    thanks again guys~! I think i solved it!
    You guys are awsome!

  13. #13
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Thank you, come again.

  14. #14
    mamoonrizwan is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default random phone number generator

    thats my solution to this problem

    // using random number generator
    import java.util.Random;

    public class chap2Project13
    {
    public static void main(String args[])
    {
    Random A = new Random();
    int num1,
    num2,
    num3,
    num4,
    num5,
    num6,
    num7,
    num8,
    num9,
    num10;


    num1 = A.nextInt(8);
    num2 = A.nextInt(8);
    num3 = A.nextInt(8);
    num4 = A.nextInt(8);
    num5 = A.nextInt(5);
    num6 = A.nextInt(3);
    num7 = A.nextInt(2);
    num8 = A.nextInt(2);
    num9 = A.nextInt(2);
    num10 = A.nextInt(2);
    System.out.println("the random phone number is " + num1 + num2 + num3 +"-" + num4 + num5 + num6 + "-" + num7 + num8 +num9 + num10);
    }




    }

Similar Threads

  1. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 08:18 PM
  2. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 12:10 PM
  3. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM
  4. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 06:43 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
  •