Results 1 to 16 of 16
Thread: Random Phone number generator
 04112011, 05:09 AM #1Member
 Join Date
 Mar 2011
 Posts
 35
 Rep Power
 0
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 XXXXXXXXXX.
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)
 04112011, 05:44 AM #2Java Code:
first0.replace('8', random0);
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.
 04112011, 05:55 AM #3Member
 Join Date
 Mar 2011
 Posts
 35
 Rep Power
 0
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.
 04112011, 06:01 AM #4
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);
 04112011, 06:15 AM #5
Why don't you use a loop?
Java Code:for (i <= 3) { random0 = generator.nextInt(8); myString = myString.concat(Integer.toString(random0)); }
 04112011, 06:17 AM #6Member
 Join Date
 Mar 2011
 Posts
 35
 Rep Power
 0
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?
 04112011, 06:17 AM #7
Yep, that was my second suggestion. Probably a lot easier.
 04112011, 06:24 AM #8Member
 Join Date
 Mar 2011
 Posts
 35
 Rep Power
 0
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
 04112011, 06:24 AM #9
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);
 04112011, 06:53 AM #10Member
 Join Date
 Mar 2011
 Posts
 35
 Rep Power
 0
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?
 04112011, 06:55 AM #11
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));
 04112011, 07:02 AM #12Member
 Join Date
 Mar 2011
 Posts
 35
 Rep Power
 0
thanks again guys~! I think i solved it!
You guys are awsome!
 04112011, 07:07 AM #13
Thank you, come again.
 04202011, 09:47 AM #14Member
 Join Date
 Apr 2011
 Posts
 19
 Rep Power
 0
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);
}
}
 05192014, 11:21 AM #15Member
 Join Date
 May 2014
 Posts
 1
 Rep Power
 0
Re: Random Phone number generator
You can use these softwares to generate random phone numbers
Mobile number generator software random number generator
Random number generator Random no generation Number generator
 05192014, 12:37 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,537
 Blog Entries
 7
 Rep Power
 20
Similar Threads

Help with Random Number Generator
By celtics in forum New To JavaReplies: 0Last Post: 03072011, 08:18 PM 
Random number generator
By zerwik in forum New To JavaReplies: 3Last Post: 12262010, 12:10 PM 
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04022010, 06:47 PM 
Help with class project, random number generator.
By Christopher The Great in forum New To JavaReplies: 4Last Post: 03142009, 02:12 AM 
Random Generator
By padutch2 in forum New To JavaReplies: 1Last Post: 12032007, 06:43 PM
Bookmarks