1. Member
Join Date
Oct 2009
Location
Posts
14
Rep Power
0

## Random class

Hey all, I have a really beginner question here but I can't seem to figure out how to get the result i'm looking for.

This is the question:

Write an application, called PhoneNumbers, that creates and
prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the
output. The phone number has some constraints. Do not let the first three digits contain
an 8 or 9 (but do not be more restrictive than that), and ensure that the second set of three
digit is not greater than 635. Note that any of the digits can be zero.

and this is the code I have so far:

public class PhoneNumbers {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int one, two, three, four, five, six, seven, eight, nine, ten;

Random generator = new Random();

one = generator.nextInt(8);
two = generator.nextInt(8);
three = generator.nextInt(8);
four = generator.nextInt(7);
five = generator.nextInt(4);
six = generator.nextInt(6);
seven = generator.nextInt(10);
eight = generator.nextInt(10);
nine = generator.nextInt(10);
ten = generator.nextInt(10);

System.out.println("Randomly Generated Phone Number: " + one + two
+ three + "-" + four + five + six + "-" + seven + eight + nine
+ ten);

}

}

my question is, how do I do the second part of 000-XXX-0000? I've tried XXX = generator.nextInt(636); .. but then it'll sometimes print a <100 number and be for example 88 instead of 088.

so my question is, how do I make it so it keeps the digits? thank you for your time :)

2. How do you import the random class?

3. Member
Join Date
Sep 2009
Posts
10
Rep Power
0
XXX = generator.nextInt(636) + 100;

Just add that + 100 on there, but then reduce your 636 to 536. Hope that solves your problems!

4. no like at the very beginning when you type import. whats the import?

5. Member
Join Date
Sep 2009
Posts
10
Rep Power
0
The import is
Java Code:
`import java.util.Random;`

6. Thanks man

7. Member
Join Date
Oct 2009
Location
Posts
14
Rep Power
0
Originally Posted by Kaito
XXX = generator.nextInt(636) + 100;

Just add that + 100 on there, but then reduce your 636 to 536. Hope that solves your problems!
I thought of that, but then wouldn't it be 100-635? I need it to be 000 to 635 :/

8. Member
Join Date
Oct 2009
Posts
3
Rep Power
0
import java.util.Random;

public class RandomGenerator {
private String one, two, three, four, five, six, seven, eight, nine, ten;
private Random objRandom = null;
private static String notMoreThan = null;

public RandomGenerator() {
// TODO Auto-generated constructor stub
objRandom = new Random();
}
public String getFirstSet(){

one = Integer.toString(objRandom.nextInt(8));
two = Integer.toString(objRandom.nextInt(8));
three = Integer.toString(objRandom.nextInt(8));
return one+two+three;
}

public String getSecondSet(){
four = Integer.toString(objRandom.nextInt(10));
five = Integer.toString(objRandom.nextInt(10));
six = Integer.toString(objRandom.nextInt(10));
return four+five+six;
}

public String getThirdSet(){
seven = Integer.toString(objRandom.nextInt(10));
eight = Integer.toString(objRandom.nextInt(10));
nine = Integer.toString(objRandom.nextInt(10));
ten = Integer.toString(objRandom.nextInt(10));
return seven+eight+nine+ten;
}

public static void main(String[] args) {
RandomGenerator objRandomGenerator = new RandomGenerator();
notMoreThan = "635";
String setOne, setTwo, setThree;
for (int i = 0; i < 100; i++) {
setOne = objRandomGenerator.getFirstSet();
setTwo = objRandomGenerator.getSecondSet();
setThree = objRandomGenerator.getThirdSet();
while (true) {
if (Integer.parseInt(setTwo) > Integer.parseInt(notMoreThan)) {
setTwo = objRandomGenerator.getSecondSet();
} else {
break;
}
}
System.out.println("The phone number is " + setOne + "-" + setTwo
+ "-" + setThree);
}
}

}

9. Have you tried the String.format method?

Java Code:
```Random random = new Random();

// a number between 0 and 99
int val = random.nextInt(100);

// outputs the number as three digits (with leading zeros, if necessary)
System.out.println(String.format("%03d", val));```

You can also use the format method to format the entire output, the three numbers (including dashes).

Java Code:
```int num1 = 1;
int num2 = 2;
int num3 = 3;

// uses same syntax as the String.format method
// (all based on the Formatter class)
// change to the format you need - adding formatting options, as necessary
System.out.printf("(%d) %d*%d", num1, num2, num3);```

10. Member
Join Date
Oct 2009
Location
Posts
14
Rep Power
0
thank you everyone for your help, appreciate it :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•