Results 1 to 19 of 19
 05132011, 08:05 PM #1Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
Need your opinions on my "Random phone number generator"
Hi, this is actually the second time in my life I have had to think like a programmer. It was great fun. I was wondering if anyone can judge my work and tell me what you think please? The program runs fine, but I don't know if this was an efficient or elegant solution. Thank you! Derek:D
Here is the code, it is pretty self explanatory.
Java Code:import java.util.Random; public class RandomPhoneNum { public static void main(String[] args) { System.out.println("This app prints out a random phone number."); System.out.println("The first set of 3 digits can't have 8 or 9 in them."); System.out.println("The second set of 3 digits can't be greater than 742."); Random generator = new Random(); int num1 = 0; int num2 = 0; int num3 = 0; num1 = generator.nextInt(600) + 100;//numbers can't include an 8 or 9, can't go below 100. num2 = generator.nextInt(641) + 100;//number has to be less than 742//can't go below 100. num3 = generator.nextInt(8999) + 1000; // make numbers 0 through 9 for each digit.//can't go below 1000. System.out.println("Here is a random generated phone number: " + num1 + "" + num2 + "" + num3); } }
Last edited by silverglade; 05132011 at 08:11 PM.
 05132011, 08:17 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,395
 Blog Entries
 7
 Rep Power
 25
 05132011, 08:26 PM #3Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
Wow Jos very smart. Good eye. I stink at math, that's going to have to change. Anyway, the book does not show me how to turn these ints into strings, and then set a pattern for the string that meets the criteria, if that is even possible I don't know. Given what the book has taught me so far, that is the best I can come up with. oh well. LOL. Thank you
 05132011, 08:33 PM #4
An int (or any other number) is very easy to turn into a string: Just use "String s=""+i". That, along with some Stringmethods, should allow you to detect if there's an 8 or 9 in there.
 05132011, 08:35 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,395
 Blog Entries
 7
 Rep Power
 25
There are many ways to turn a number into its String representation; read the API documentation for the String class for starters (the valueOf( ... ) method is of great help here). The Integer class also has a few methods available and if all else fails the trick ""+number helps you out (it plays a few tricks behind the scenes).
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 05132011, 09:02 PM #6Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
thanks, adding this code what the best I could come up with so far. I can't figure out how to say "is there an 8 or 9 in one of the 3 numbers? If so, make it a zero."
but this is what I did to convert the 3 digits that can't contain an 8 or 9 to a string.
Java Code:String string1 = Integer.toString(num1);
 05132011, 09:06 PM #7
Have a look at String (Java Platform SE 6) and see if anything pops out at you.
 05132011, 09:07 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,395
 Blog Entries
 7
 Rep Power
 25
Here's a hint: an octal number doesn't contain 8s or 9s in it. You effectively want an octal number in the range 100 ... 777 (octal). A random number generator can generate numbers in the range 0 ... 677 (octal again) and when you add 100 (also octal) to it, you've got your number. The Integer.getOctalString( ... ) can do the last part ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 05132011, 09:29 PM #9Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
Thank you Jos. For the first time I know to not depend on my books but depend on the API documentation since no book has it all. This is the code I came up with based on what you told me so far. Thank you for helping me. I get the following error though when trying to compile.
C:\JAVA_PROGRAMMING_CODE\code1\RandomPhoneNum>java c RandomPhoneNum.java
RandomPhoneNum.java:23: cannot find symbol
symbol : method toOctalString(int)
location: class RandomPhoneNum
strippedNum = toOctalString(num1);
^
1 error
here is the code so far
Java Code:import java.util.Random; public class RandomPhoneNum { public static void main(String[] args) { System.out.println("This app prints out a random phone number."); System.out.println("The first set of 3 digits can't have 8 or 9 in them."); System.out.println("The second set of 3 digits can't be greater than 742."); Random generator = new Random(); String strippedNum; int num1 = 0; int num2 = 0; int num3 = 0; num1 = generator.nextInt(600) + 100;//numbers can't include an 8 or 9, can't go below 100. num2 = generator.nextInt(641) + 100;//number has to be less than 742//can't go below 100. num3 = generator.nextInt(8999) + 1000; // make numbers 0 through 9 for each digit.//can't go below 1000. String string1 = Integer.toString(num1); strippedNum = toOctalString(num1); System.out.println("Here is a random generated phone number: " + strippedNum + "" + num2 + "" + num3); } }
I just posted this question on another forum here
Random phone number generator help please (Beginning Java forum at JavaRanch)Last edited by silverglade; 05132011 at 10:22 PM.
 05142011, 08:59 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,395
 Blog Entries
 7
 Rep Power
 25
That still isn't correct; if you want to do it the octal way as I described, do this:
Java Code:num1= generator.nextInt(0700)+0100;
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 05142011, 12:32 PM #11Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
thank you VERY much Jos. That was bothering me all night and when I woke up , bang, your answer was there. yay! But I still never would have thought of it. This problem from the book bothered me so much, and the fact that I didn't have a clue, made me buy like 11 math and programming books last night. I was wondering if you think it will help me not run into problems like this again? I am going to look at wikipedia about different base numbers, and the "code" book should cover the number types. Here are the books that I bought. Please let me know if you think that is enough math to be a good programmer (as far as math goes).
1 Algebra the Easy Way
Douglas Downing
2 EZ Precalculus (Barron's EZ)
Lawrence Leff
3 Trigonometry the Easy Way
Douglas Downing Ph.D.
4 Schaum's Outline of Discrete Mathematics, 3rd Ed. (Schaum's Outline Series)
Seymour Lipschutz, Marc Lipson
5 Math Word Problems the Easy Way
David Ebner
6 Arithmetic the Easy Way Arithmetic the Easy Way
7 Statistics The Easy Way (Easy Way Series)
8 EZ Geometry
Lawrence S. Leff
Ph.D. Douglas Downing, Jeff Clark Ph.D.
9 Calculus the Easy Way (Barron's EZ Calculus)
Douglas Downing Ph.D.
10 Introduction to Algorithms, Second Edition Introduction to Algorithms, Second Edition
Thomas H Cormen, et al
11 Code (DVMPS General) Code (DVMPS General)
Charles Petzold
 05142011, 12:43 PM #12
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
That's a lot of books, careful not to overwhelm yourself. Make sure you focus and don't skip around too much.
Another approach to your random number could be to generate them singularly when you need to limit what they are.
If the original three numbers can't have 7s you cloud generate three numbers in the range 09, if the number generated is a 7 re "roll". This may be a more naive approach but it was the first thing that jumped to my head when I did that problem.
 05142011, 12:55 PM #13Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
thanks sunde887. The book had me thinking in terms of 3 digit numbers to start, I wonder if they didn't tell me that if I would have thought of doing singles. One thing is for sure, I need to start thinking better like a programmer, creatively. My mind is a baby at that, I hope I improve. Your technique is good. One that I might have been able to come up with, but now I know octal and different base numbers, hopefully I will be able to use it in code solutions. I don't like depending on the forum to answer my problems, that sucks.
I think it was wrong for the book to put in such a question that it didn't cover the aspects of the solution prior. Almost every programming book I have ever read does that. It does teach me to search for my answer, but it would be nice if they didn't do that in the first place. I will have to work harder at creative problem solving, I just hope I get better at it. With this particular problem, I felt like a monkey banging my head against the wall, very unpleasant.
Yes I will focus on one book at a time, in the order it is taught in school.
 05142011, 12:59 PM #14
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
It's frustrating when they don't quite cover the topics but that's generally what makes more challenging exercises.
 05142011, 01:04 PM #15Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
yes it is very frustrating. They didn't even cover if statements yet LOL. Usually what happens is I end up begging the forum in a situation like that after I tried my best, I HATE having to do that. After I know enough math it should go smoother. And once I cover most of the topics by the end of the book. I want it to get to the point that all I need to "fix" is the weakness of my logic. I don't ever want to run into problems again because of math ignorance.
 05142011, 02:51 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,395
 Blog Entries
 7
 Rep Power
 25
 05142011, 05:48 PM #17
If you can't get the octal method to work, you can always just randomise three digits with the right intervals instead and combine them.
 05142011, 05:55 PM #18Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
ok thank you Toll.
 05142011, 06:02 PM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,395
 Blog Entries
 7
 Rep Power
 25
Similar Threads

Random Phone number generator
By elecleoalune in forum New To JavaReplies: 15Last Post: 05192014, 12:37 PM 
College "java" paper.....a couple opinions?
By hayden06f4i in forum New To JavaReplies: 6Last Post: 11012010, 08:15 AM 
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04022010, 06:47 PM 
how to sort "name" arraylist with its phone & address?
By anthrax in forum New To JavaReplies: 2Last Post: 02022009, 12:29 PM 
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10202008, 07:35 AM
Bookmarks