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
251 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now