# phone number generator

• November 3rd, 2013, 08:09 PM
yuli
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!!!
• November 3rd, 2013, 08:22 PM
Norm
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]
[/code]
to get highlighting and preserve formatting.
• November 3rd, 2013, 09:07 PM
yuli
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
• November 3rd, 2013, 09:21 PM
Norm
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]
[/code]
to get highlighting and preserve formatting.
• November 3rd, 2013, 09:35 PM
yuli
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
• November 3rd, 2013, 09:40 PM
Norm
Re: phone number generator
Please edit your post and wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.
• November 3rd, 2013, 09:42 PM
yuli
Re: phone number generator
sorry didn't see it
• November 3rd, 2013, 10:03 PM
Norm
Re: phone number generator
Quote:

still won't work

Please edit your post and wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.
• November 3rd, 2013, 11:08 PM
yuli
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.
• November 4th, 2013, 04:21 PM
yuli
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);

}
}
• November 4th, 2013, 04:24 PM
Norm
Re: phone number generator
Please edit your post and wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.
• November 4th, 2013, 04:36 PM
yuli
Re: phone number generator
how what you mean sorry?.. how?
• November 4th, 2013, 04:41 PM
Norm
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]
• November 4th, 2013, 05:05 PM
yuli
Re: phone number generator
Code java:

```/*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);     } }```
• November 4th, 2013, 05:12 PM
Norm
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.
• November 4th, 2013, 05:12 PM
yuli
Re: phone number generator
ohhh I see what you meant hopefully now its better
• November 4th, 2013, 05:14 PM
Norm
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
• November 4th, 2013, 05:30 PM
yuli
Re: phone number generator
Code java:

```/*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?
• November 4th, 2013, 05:36 PM
Norm
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.