# Random class

• 10-27-2009, 04:20 AM
KM88
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 :)
• 10-27-2009, 04:23 AM
SwEeTAcTioN
How do you import the random class?
• 10-27-2009, 04:24 AM
Kaito
XXX = generator.nextInt(636) + 100;

Just add that + 100 on there, but then reduce your 636 to 536. Hope that solves your problems!
• 10-27-2009, 04:32 AM
SwEeTAcTioN
no like at the very beginning when you type import. whats the import?
• 10-27-2009, 05:14 AM
Kaito
The import is
Code:

`import java.util.Random;`
• 10-27-2009, 05:19 AM
SwEeTAcTioN
Thanks man
• 10-27-2009, 05:23 AM
KM88
Quote:

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 :/
• 10-27-2009, 12:29 PM
freakydeveloper
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);
}
}

}
• 10-27-2009, 08:10 PM
CodesAway
Have you tried the String.format method?

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).

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-27-2009, 09:55 PM
KM88
thank you everyone for your help, appreciate it :)