# Random Phone number generator

• 04-11-2011, 05:09 AM
elecleoalune
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)
• 04-11-2011, 05:44 AM
Junky
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.
• 04-11-2011, 05:55 AM
elecleoalune
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.
• 04-11-2011, 06:01 AM
Junky
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.
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.
• 04-11-2011, 06:15 AM
Dark
Why don't you use a loop?
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.
• 04-11-2011, 06:17 AM
elecleoalune
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?
• 04-11-2011, 06:17 AM
Junky
Yep, that was my second suggestion. Probably a lot easier.
• 04-11-2011, 06:24 AM
elecleoalune
oh thanks guys~
the reason why i didn't use loop was because the textbook still haven't gone through that far yet...
I'll try using the above example and get back to you guys ASAP
• 04-11-2011, 06:24 AM
Dark
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.

Code:

`int myInteger = Integer.parseInt(myString);`
• 04-11-2011, 06:53 AM
elecleoalune
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?
• 04-11-2011, 06:55 AM
Dark
Do it the inefficient way.

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.
• 04-11-2011, 07:02 AM
elecleoalune
thanks again guys~! I think i solved it!
You guys are awsome!
• 04-11-2011, 07:07 AM
Dark
Thank you, come again.
• 04-20-2011, 09:47 AM
mamoonrizwan
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);
}

}
• 05-19-2014, 11:21 AM
karanshrof2
Re: Random Phone number generator
• 05-19-2014, 12:37 PM
JosAH
Re: Random Phone number generator