# Random Phone Numbers

Page 1 of 1

## 6 Replies - 17450 Views - Last Post: 05 December 2010 - 02:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=203790&amp;s=80a0208eec6db69e5a1da1b99d87f06a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 doonieb1

Reputation: 0
• Posts: 4
• Joined: 05-December 10

# Random Phone Numbers

Posted 05 December 2010 - 01:16 PM

The question is : 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.

I am having trouble with any leading zeroes being printed,

also with ac1, ac2, ac3 showing as num1 (but not added together).

here is my code:
```import java.util.Random;

public class phoneNumber

{

//---------------------------------------------------------

//This program will generate a random 10 digit phone number

//---------------------------------------------------------

public static void main(String[] args)

{

Random generator = new Random();

//integers ac1, ac2, ac3 = 3 number making up the area code

//num1 = Area Code, num2 = prefix, num3 = suffix

int ac1;

int ac2;

int ac3;

int num1;

int num2;

int num3;

ac1 = generator.nextInt(8);

ac2 = generator.nextInt(8);

ac3 = generator.nextInt(8);

num1 = (ac1 + ac2 + ac3);

{

if (num1 < 10)

num1 = (+00 + num1);

if (num1 < 100)

num1 = (+0 + num1);

}

num2 = generator.nextInt(743);

{

if (num2 < 100)

num2 = (+0+ num2);

if (num2 < 10)

num2 = (+00 + num2);

}

num3 = generator.nextInt(10000);

{

if (num3 < 1000)

num3 = (+0 + num3);

if (num3 <= 100);

num3 = (+00 + num3);

if (num3 <= 10);

num3 = (+000 + num3);

}

System.out.println("Your random phone number is " + num1 + "-" + num2 + "-" + num3);

}

}

```

example output:

Your random phone number is 5-88-3951

This post has been edited by macosxnerd101: 05 December 2010 - 01:31 PM
Reason for edit:: Please use code tags. Also, title renamed to be more descriptive. "Java Homework" describes 95% of the posts in this forum.

Is This A Good Question/Topic? 0

## Replies To: Random Phone Numbers

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Random Phone Numbers

Posted 05 December 2010 - 01:27 PM

You are complication your life for nothing
Take second number that should be between 100 and 742 so
num2 = generator.nextInt(643) + 100;

num3 is obviously = generator.nextInt(10000);

as far as the area code
firstDigit should be between 1-7 so generator.nextInt(7) + 1
secondDigit should be between 0-7 so generator.nextInt(8);
same thing for thirdDigit
and area will be (firstDigit * 100) + (secondDigit * 10) + thirdDigit

### #3 doonieb1

Reputation: 0
• Posts: 4
• Joined: 05-December 10

## Re: Random Phone Numbers

Posted 05 December 2010 - 01:35 PM

pbl, on 05 December 2010 - 12:27 PM, said:

You are complication your life for nothing
Take second number that should be between 100 and 742 so
num2 = generator.nextInt(643) + 100;

num3 is obviously = generator.nextInt(10000);

as far as the area code
firstDigit should be between 1-7 so generator.nextInt(7) + 1
secondDigit should be between 0-7 so generator.nextInt(8);
same thing for thirdDigit
and area will be (firstDigit * 100) + (secondDigit * 10) + thirdDigit

"I understand what you are stating, but my instructor wants us to write the code with the instance of creating numbers with leading zeroes. I attempted to suggest the route that you explained, but he insisted that the question did not state to exclude numbers such as: 001, 012, 0001, 0012, 0123, etc."

### #4 calvinthedestroyer

• D.I.C Lover

Reputation: 172
• Posts: 1,932
• Joined: 13-October 07

## Re: Random Phone Numbers

Posted 05 December 2010 - 01:44 PM

Can you format your output as so that the 0's do not get truncated?

(I don't know Java) Will something like this work:
```System.out.format("%08d%n", n);                //  -->  "00461012"

```

I found that here: java output formats

maybe
```System.out.format.println("%08d%n", n);
```

### #5 doonieb1

Reputation: 0
• Posts: 4
• Joined: 05-December 10

## Re: Random Phone Numbers

Posted 05 December 2010 - 01:52 PM

calvinthedestroyer, on 05 December 2010 - 12:44 PM, said:

Can you format your output as so that the 0's do not get truncated?

(I don't know Java) Will something like this work:
```System.out.format("%08d%n", n);                //  -->  "00461012"

```

I found that here: java output formats

maybe
```System.out.format.println("%08d%n", n);
```

We have not yet learned any formatting commands such as that.

I was wondering if converting the phone number to a string would work, but I am not sure if I would get any points marked off for the conversion since we are working only with randomly generating numbers.

I know that you can concatenate strings, but I don't know how to create the output of the leading zeroes without the formatting option you have suggested. I think that my instructor would question my results, because we have not yet learned that code command yet.

I don't know - I may just have him show me how he wants it done. I just wanted it to work befor I submitted it. He is too literal when reading the HW assignments. Phone numbers are not produced in the way he is asking us to write the code, I pointed that out, but he explicitly stated that the HW question did not state to exlude leading zeroes.

### #6 doonieb1

Reputation: 0
• Posts: 4
• Joined: 05-December 10

## Re: Random Phone Numbers

Posted 05 December 2010 - 02:10 PM

@pbl
@calvinthedestroyer

Guys I appreciate the assistance.

@pbl - I am going to utilize you example, if I get marked off so be it.

Thanks again guys.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Random Phone Numbers

Posted 05 December 2010 - 02:39 PM