Results 1 to 19 of 19
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default 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; 05-13-2011 at 09:11 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by silverglade View Post
    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);
    	}
    
    }
    Suppose the first random number being generated is 599; adding 100 results in 699. That number is not supposed to have an 8 or 9 in it according to the specs.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    An int (or any other number) is very easy to turn into a string: Just use "String s=""+i". That, along with some String-methods, should allow you to detect if there's an 8 or 9 in there.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by silverglade View Post
    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
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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);

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Have a look at String (Java Platform SE 6) and see if anything pops out at you.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by silverglade View Post
    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);
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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);
    	}
    
    }
    Also, how did you know about octal base numbers? I would never have known that. Do you know what I can do to improve my math ability like you have please? I desperately need it. like any books you know? please. thanks.

    I just posted this question on another forum here

    Random phone number generator help please (Beginning Java forum at JavaRanch)
    Last edited by silverglade; 05-13-2011 at 11:22 PM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by silverglade View Post
    Java Code:
    num1 = generator.nextInt(600) + 100;//numbers can't include an 8 or 9, can't go below 100.
    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;
    Pay close attention to those numbers 0700 and 0100; they're numbers in octal notation. The expression Integer.toOctalString(num1) gives you a String in octal notation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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 E-Z Precalculus (Barron's E-Z)
    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 E-Z Geometry
    Lawrence S. Leff
    Ph.D. Douglas Downing, Jeff Clark Ph.D.


    9 Calculus the Easy Way (Barron's E-Z Calculus)
    Douglas Downing Ph.D.

    10 Introduction to Algorithms, Second Edition Introduction to Algorithms, Second Edition
    Thomas H Cormen, et al

    11 Code (DV-MPS General) Code (DV-MPS General)
    Charles Petzold

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 0-9, 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.

  13. #13
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's frustrating when they don't quite cover the topics but that's generally what makes more challenging exercises.

  15. #15
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sunde887 View Post
    If the original three numbers can't have 7s you cloud generate three numbers in the range 0-9, 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.
    The Random.nextInt(n) method helps you out by generating a non-negative int less than its parameter n. There's no need to 'reroll' ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    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.

  18. #18
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    ok thank you Toll.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Toll View Post
    If you can't get the octal method to work, [ snip ... ]
    That would be goofy.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Random Phone number generator
    By elecleoalune in forum New To Java
    Replies: 15
    Last Post: 05-19-2014, 01:37 PM
  2. College "java" paper.....a couple opinions?
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-01-2010, 08:15 AM
  3. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 07:47 PM
  4. Replies: 2
    Last Post: 02-02-2009, 12:29 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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