7 Replies - 1142 Views - Last Post: 04 October 2012 - 09:59 PM Rate Topic: -----

#1 mkorb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 12

Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:13 PM

Using the Random class, write a program that generates a random phone number of the form XXX-XXX-XXXX where the first group of three digits may not start with a 0 or a 1, the middle three digits must form an even number between 000 and 998, and the last group of four digits must be between 0000 and 5999. Your program may place no other restrictions on any of the groups of numbers. Exactly 10 digits and the two dashes must be printed.

For some reason I can't get the middle numbers to be all even and the first three numbers I can't get to be different. Here is what i have so far...
import java.util.Random;

public class PhoneNumber 
{

	public static void main(String args[]) 
	
	{
		Random generator = new Random();
		int num1,num2,num3;
		
		
		
		
		num1 = generator.nextInt(9) + 2 ; // returns random number between 0 and 7
		num1 = generator.nextInt(7) - 3;
		num1 = generator.nextInt(6) + 4;
		num2 = generator.nextInt(999) - 2 ; // returns random number between 0 and 741
		num3 = generator.nextInt(6000); // returns random number between 0 and 9999

		String phoneNumber = String.format("%d%d%d-%03d-%04d", num1, num1, num1, num2, num3);
		System.out.println(phoneNumber);
	      
	      
	      
		
		}

This post has been edited by jon.kiparsky: 04 October 2012 - 12:16 PM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Creating Phone Numbers with Random Class

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,726
  • Joined: 19-March 11

Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:21 PM

You have some incorrect comments in your code.

num1 = generator.nextInt(9) + 2 ; // returns random number between 0 and 7


No it doesn't. It generates a random number n, such that 0 <= n <9 and then it adds 2 to it.

	num2 = generator.nextInt(999) - 2 ; // returns random number between 0 and 741


Where does 741 come from?


Quote

For some reason I can't get the middle numbers to be all even


One way to think about this is to generate random numbers and test them until you get an even one.
Another way is to create a random number that you know will be even.

Here's a hint: how many three-digit even numbers are there? Can they be mapped onto any other set of integers?
Was This Post Helpful? 1
  • +
  • -

#3 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5189
  • View blog
  • Posts: 26,896
  • Joined: 10-May 07

Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:23 PM

Please be mindful of your topic when posting. Being that this is a support forum, in most cases members are posting questions.

I have updated the title to be more descriptive of the question.
Was This Post Helpful? 0
  • +
  • -

#4 mkorb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 12

Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:25 PM

Sorry about that when I was changing things around trying to fix it, I ignored changing the comments.
import java.util.Random;

public class PhoneNumber 
{

	public static void main(String args[]) 
	
	{
		Random generator = new Random();
		int num1,num2,num3;
		
		
		
		
		num1 = generator.nextInt(9) + 2 ; // first group may not start with 0 or a 1
		num1 = generator.nextInt(7) + 2;
		num1 = generator.nextInt(6) + 2;
		num2 = generator.nextInt(999) - 2 ; //must form an even number between 000-998
		num3 = generator.nextInt(6000); //must be between 0000-5999

		String phoneNumber = String.format("%d%d%d-%03d-%04d", num1, num1, num1, num2, num3);
		System.out.println(phoneNumber);

This post has been edited by jon.kiparsky: 04 October 2012 - 12:29 PM
Reason for edit:: added code tags (again)

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,726
  • Joined: 19-March 11

Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:29 PM

View Postmkorb, on 04 October 2012 - 02:25 PM, said:

Sorry about that when I was changing things around trying to fix it, I ignored changing the comments.


As a side note, this is a good reason to beware of comments of this sort. They tend to get out of step with the code. I can tell what you're doing, tell me why you're doing it.

This post has been edited by jon.kiparsky: 04 October 2012 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 07:20 PM

what is the use of regenerating 3 times the value stored in num1 ???

num1 = generator.nextInt(9) + 2 ; // first group may not start with 0 or a 1
num1 = generator.nextInt(7) + 2;
num1 = generator.nextInt(6) + 2;

only the last one will be kept
Was This Post Helpful? 0
  • +
  • -

#7 mkorb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 12

Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 09:49 PM

num1 = generator.nextInt(9)+2 ;
num2 = generator.nextInt(999) ; //must form an even number between 000-998
num3 = generator.nextInt(6000); //must be between 0000-5999

String phoneNumber = String.format("%d%d%d-%03d-%04d", num1, num2, num3);
System.out.println(phoneNumber);

whenever i try to run it, it gives me an error message and it's because of the format, when i try to change %d to %03d it gives me a 0 as the first number in the start of the phone number which is not allowed as it is stated in the instructions. I can't figure out how to make the first three numbers not start with 0 or 1 and I can't figure out how to make the middle 3 all even numbers, in other words how to I rule out odd numbers?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,726
  • Joined: 19-March 11

Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 09:59 PM

Quote

I can't figure out how to make the middle 3 all even numbers, in other words how to I rule out odd numbers?


One way would be to generate numbers until you get one which meets the definition of "even". There are several definitions you could use, but the most useful one is "is evenly divisible by 2". That's going to require a while loop and an unknown amount of cpu time - not much, in any case, but certainly more than you need to use, because you can use another approach - again, think about the definition of "even", and think about how many even numbers there are between zero and 1000.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1