Results 1 to 10 of 10
Thread: Random class
- 10-27-2009, 03:20 AM #1
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
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 :)
- 10-27-2009, 03:23 AM #2
How do you import the random class?
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 10-27-2009, 03:24 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
XXX = generator.nextInt(636) + 100;
Just add that + 100 on there, but then reduce your 636 to 536. Hope that solves your problems!
- 10-27-2009, 03:32 AM #4
no like at the very beginning when you type import. whats the import?
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 10-27-2009, 04:14 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
The import is
Java Code:import java.util.Random;
- 10-27-2009, 04:19 AM #6
Thanks man
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 10-27-2009, 04:23 AM #7
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
- 10-27-2009, 11:29 AM #8
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
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);
}
}
}
- 10-27-2009, 07:10 PM #9
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-27-2009, 08:55 PM #10
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
how i use the random class to random the cards i have
By yanipao in forum New To JavaReplies: 14Last Post: 10-19-2009, 10:57 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Help with class project, random number generator.
By Christopher The Great in forum New To JavaReplies: 4Last Post: 03-14-2009, 02:12 AM -
Using Random
By razmyasdfg in forum CLDC and MIDPReplies: 1Last Post: 07-27-2008, 10:47 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks