Sign in with
Sign up | Sign in
Your question

Beginner`s JAVA Random question

Tags:
  • Programming
  • Phones
  • Java
  • Apps
Last response: in Apps General Discussion
Share
April 2, 2012 4:39:59 PM

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:
  1. import java.util.Random;
  2.  
  3. public class PhoneNumGen
  4. {
  5. public static void main (String[] args)
  6. {
  7. Random generator = new Random();
  8. int dig1, dig2, dig3, digSet2, digSet3;
  9. String phoneNumber;
  10.  
  11. dig1 = generator.nextInt(8);
  12. dig2 = generator.nextInt(8);
  13. dig3 = generator.nextInt(8);
  14. digSet2 = generator.nextInt(743);
  15. digSet3 = generator.nextInt (10000);
  16.  
  17. phoneNumber = (+dig1 +"" +dig2 +"" +dig3 +"-" + digSet2 +"-" + digSet3);
  18.  
  19. System.out.println ("Random phone number generated: " + phoneNumber);
  20. }
  21. }


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

More about : beginner java random question

a b L Programming
April 3, 2012 8:50:37 AM

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.
m
0
l
April 3, 2012 10:05:56 PM

Thanks,
I thought I was missing something.
m
0
l
!