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
259 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

685 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