Solved

How to create a java program that generates and prints a random phone number in the form xxx-xxx-xxxx

Posted on 2004-10-12
9
254 Views
Last Modified: 2012-08-14
Thats basically what i need to know in a nutshell.....
these are the parameters
-The first digit can't be a zero
-The first 3 digits can't contain an 8 or 9
-the second set of 3 digits cant begin with a zero
-the second set of 3 digits cant be greater than 610
-The first digit of the last set of digits cant be a zero

(ultimately i need it written in java form)
0
Comment
Question by:vsands
  • 6
9 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12286275
What do you have already?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12286292
We're not here to write a complete programs for you. (Lots of other sites exist for that)
We're here to help you with problems you might have when writing your program
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12286313
Use java.util.Random.nextInt() to generate the parts of your telephone number
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12286387
This can give you an idea:

String digits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
StringBuffer telephoneNr = new StringBuffer();
Random rd = new Random();

// Generate first digit (which shouldn't be 0, 8 or 9)
String tmp;
do {
   tmp = digits[ rd.nextInt(8) ];
} while ( !tmp.equals("0") );
telephoneNr.append(tmp);

for (int i=1; i<=2; i++) { // Generate 2nd/3rd digit (which shouldn't be 8 or 9)
    do {
       tmp = digits[ rd.nextInt(10) ];
    } while ( !tmp.equals("0") );
    telephoneNr.append(tmp);
}

...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12286400
Typo:

    tmp = digits[ rd.nextInt(10) ];

should be

    tmp = digits[ rd.nextInt(8) ];   // since 8 and 9 shouldn't be "picked"
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12286503
Or easier:

StringBuffer telephoneNr = new StringBuffer();
Random rd = new Random();

// Generate first part
String tmp;
do {
   tmp = "" + (100 + rd.nextInt(900));
} while ( tmp.indexOf("8")!=-1 || tmp.indexOf("9")!=-1 );
telephoneNr.append(tmp);

telephoneNr.append("-");

// Generate 2nd part
tmp = "" + (100 + rd.nextInt(511));
telephoneNr.append(tmp);

telephoneNr.append("-");

// Generate 3rd part
tmp = "" + (1000 + rd.nextInt(9000));
telephoneNr.append(tmp);

System.out.println( telephoneNr.toString() );
0
 
LVL 2

Expert Comment

by:shah1d1698
ID: 12321679
Try this code.. Compile & run the program.


public class Application1
{

  String finalString = null;
  public void generateNumber()
  {
    int p1 = 0, p2 = 0, p3 = 0;
    String s1 = null, s2 = null, s3 = null;


    while(true)
    {
      p1 = (int) (Math.random() * 1000);
      String temp = "" + p1;
      int tempFlag = 0;
      for(int i = 0; i < temp.length();i ++)
      {
        if(temp.charAt(i) == '8' || temp.charAt(i) == '9')
        {
          tempFlag = 1;
        }
      }
      if(p1 > 100 && tempFlag == 0)
        break;
    }

    finalString = "" + p1;

    while(true)
    {
      p2 = (int) (Math.random() * 1000);
      if(p2 > 100 && p2 < 610)
        break;
    }

    finalString = finalString + "-" + p2;

    while(true)
    {
      p3 = (int) (Math.random() * 10000);
      if(p3 > 1000)
        break;
    }

    finalString = finalString + "-" + p3;

    System.out.println(finalString);

  }

  public static void main(String args[])
  {
    Application1 app = new Application1();
    for(int i = 0; i < 1000; i++) // this will generate 1000 numbers..
    {
      app.generateNumber();
    }
  }
}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question