Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
257 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

861 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