Beginner`s JAVA Random question

ZKR

Honorable
Mar 12, 2012
35
0
10,580
0
Hello.

I have to make an app that generated a random phone number.
XXX-XXX-XXXX
There can`t be any 8 or 9 digits in the first set.
There can`t be a number higher than 742 in the second. It says that it is possible to construct the number without determining each digit separately. I couldn`t figure out how to (if possible) define the first set without defining every dig separately, and wrote it this way:
Code:
import java.util.Random;

public class PhoneNumGen		
{	
	public static void main (String[] args)
	{
	Random generator = new Random();
	int dig1, dig2, dig3, digSet2, digSet3;
	String phoneNumber;
	
	dig1 = generator.nextInt(8);
	dig2 = generator.nextInt(8);
	dig3 = generator.nextInt(8);
	digSet2 = generator.nextInt(743);
	digSet3 = generator.nextInt (10000);
	
	phoneNumber = (+dig1 +"" +dig2 +"" +dig3  +"-" + digSet2 +"-" + digSet3);
	
	System.out.println ("Random phone number generated: " + phoneNumber);
	}
}
Is there a way to do it they way the book asked? (without dig1, dig2, dig3)
(Classes introduced: String, Random, Math, NumberFormat, DecimalFormat, Wrapper, Enumerated Types, printf method).
 

Rusting In Peace

Distinguished
Jul 2, 2009
312
0
19,060
34
I don't know of any method to generate a random number that doesn't have a specific digit in it. The fact that you've optimised number generation for the second and third parts of the number means that you aren't generating random numbers for each individual digit.
 
Thread starter Similar threads Forum Replies Date
K Apps General Discussion 2
N Apps General Discussion 2
A Apps General Discussion 10
A Apps General Discussion 1
S Apps General Discussion 4
H Apps General Discussion 1
B Apps General Discussion 23
B Apps General Discussion 2
I Apps General Discussion 2
K Apps General Discussion 3
D Apps General Discussion 3
B Apps General Discussion 2
G Apps General Discussion 14
C Apps General Discussion 1
L Apps General Discussion 1
syssoft Apps General Discussion 1
E Apps General Discussion 5
T Apps General Discussion 1
U Apps General Discussion 3
C Apps General Discussion 9

ASK THE COMMUNITY