Results 1 to 10 of 10

Thread: Random class

  1. #1
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default Random class

    Hey all, I have a really beginner question here but I can't seem to figure out how to get the result i'm looking for.

    This is the question:

    Write an application, called PhoneNumbers, that creates and
    prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the
    output. The phone number has some constraints. Do not let the first three digits contain
    an 8 or 9 (but do not be more restrictive than that), and ensure that the second set of three
    digit is not greater than 635. Note that any of the digits can be zero.


    and this is the code I have so far:

    public class PhoneNumbers {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    int one, two, three, four, five, six, seven, eight, nine, ten;

    Random generator = new Random();

    one = generator.nextInt(8);
    two = generator.nextInt(8);
    three = generator.nextInt(8);
    four = generator.nextInt(7);
    five = generator.nextInt(4);
    six = generator.nextInt(6);
    seven = generator.nextInt(10);
    eight = generator.nextInt(10);
    nine = generator.nextInt(10);
    ten = generator.nextInt(10);

    System.out.println("Randomly Generated Phone Number: " + one + two
    + three + "-" + four + five + six + "-" + seven + eight + nine
    + ten);

    }

    }


    my question is, how do I do the second part of 000-XXX-0000? I've tried XXX = generator.nextInt(636); .. but then it'll sometimes print a <100 number and be for example 88 instead of 088.

    so my question is, how do I make it so it keeps the digits? thank you for your time :)

  2. #2
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    How do you import the random class?
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  3. #3
    Kaito is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    XXX = generator.nextInt(636) + 100;

    Just add that + 100 on there, but then reduce your 636 to 536. Hope that solves your problems!

  4. #4
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    no like at the very beginning when you type import. whats the import?
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  5. #5
    Kaito is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    The import is
    Java Code:
    import java.util.Random;

  6. #6
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    Thanks man
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  7. #7
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Kaito View Post
    XXX = generator.nextInt(636) + 100;

    Just add that + 100 on there, but then reduce your 636 to 536. Hope that solves your problems!
    I thought of that, but then wouldn't it be 100-635? I need it to be 000 to 635 :/

  8. #8
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    import java.util.Random;

    public class RandomGenerator {
    private String one, two, three, four, five, six, seven, eight, nine, ten;
    private Random objRandom = null;
    private static String notMoreThan = null;

    public RandomGenerator() {
    // TODO Auto-generated constructor stub
    objRandom = new Random();
    }
    public String getFirstSet(){

    one = Integer.toString(objRandom.nextInt(8));
    two = Integer.toString(objRandom.nextInt(8));
    three = Integer.toString(objRandom.nextInt(8));
    return one+two+three;
    }

    public String getSecondSet(){
    four = Integer.toString(objRandom.nextInt(10));
    five = Integer.toString(objRandom.nextInt(10));
    six = Integer.toString(objRandom.nextInt(10));
    return four+five+six;
    }

    public String getThirdSet(){
    seven = Integer.toString(objRandom.nextInt(10));
    eight = Integer.toString(objRandom.nextInt(10));
    nine = Integer.toString(objRandom.nextInt(10));
    ten = Integer.toString(objRandom.nextInt(10));
    return seven+eight+nine+ten;
    }

    public static void main(String[] args) {
    RandomGenerator objRandomGenerator = new RandomGenerator();
    notMoreThan = "635";
    String setOne, setTwo, setThree;
    for (int i = 0; i < 100; i++) {
    setOne = objRandomGenerator.getFirstSet();
    setTwo = objRandomGenerator.getSecondSet();
    setThree = objRandomGenerator.getThirdSet();
    while (true) {
    if (Integer.parseInt(setTwo) > Integer.parseInt(notMoreThan)) {
    setTwo = objRandomGenerator.getSecondSet();
    } else {
    break;
    }
    }
    System.out.println("The phone number is " + setOne + "-" + setTwo
    + "-" + setThree);
    }
    }

    }

  9. #9
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Have you tried the String.format method?

    Java Code:
    Random random = new Random();
    
    // a number between 0 and 99
    int val = random.nextInt(100);
    
    // outputs the number as three digits (with leading zeros, if necessary)
    System.out.println(String.format("%03d", val));

    You can also use the format method to format the entire output, the three numbers (including dashes).

    Java Code:
    int num1 = 1;
    int num2 = 2;
    int num3 = 3;
    
    // uses same syntax as the String.format method
    // (all based on the Formatter class)	
    // change to the format you need - adding formatting options, as necessary
    System.out.printf("(%d) %d*%d", num1, num2, num3);
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  10. #10
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM
  4. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 10:47 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •