File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Random-Format Number? Pls advise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random-Format Number? Pls advise" Watch "Random-Format Number? Pls advise" New topic

Random-Format Number? Pls advise

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
I display the random number works so far just print out the random number, and make it positve.
Please advise
1. How do I format to get xxx-xxx-xxxx
2. How do I get the first set not including 8 , 9 and the sum of 3 digit in the second set < 742.
Your help is greatly appreciated.
import java.util.Random;
import java.text.*;
public class Telephone
public static void main (String [] args)
Random generator = new Random ();
int num , formatNum, num2;
num = generator.nextInt();
//formatNum = Math.abs(num);
NumberFormat nf = NumberFormat.getInstance();
num2 = nf.parse(formatNum);

System.out.println ("A random telephone number is " + num2);
Chris De Vries
Ranch Hand

Joined: Dec 05, 2002
Posts: 65
You can generate each number in the sequence by itself, and then output the sequence in any format you want. For example:

Formatting the individual digits is easy, but the four digit sequence might be less that 1000, so you need to ensure a minimum of four digits. Use java.text.NumberFormat for that:

I hope this is helpful.
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Thanks Chris, it works
I agree. Here's the link:
subject: Random-Format Number? Pls advise
It's not a secret anymore!