• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Random-Format Number? Pls advise

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Chris
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chris, it works
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic