Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Mobile Number Generator

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Mobile Number Generator

    Hi all,

    I have created a Mobile Number Generator.

    Here are the requirements:

    - Number always starts with 07
    - Number must be 11 Digits long
    - Remaining 9 digits to be generated 0-9
    - Output to be the Complete Number as a String of Length 11.


    Here is the Code:


    package latesttasks;
     
    // Mobile phone number generator
    // Ask customer how many numbers they would like generated. 
    // Requirements: start with 07, 11 numbers, no repeats. 
    import java.util.Random;
     
    public class Task4 {
     
        public static void main(String[] args) {
     
            String numStart = "07";
            String numRemainder;
            String numComplete;
            String sd3, sd4, sd5, sd6, sd7, sd8, sd9, sd10, sd11;
     
            Random rand = new Random();
     
            int d3 = rand.nextInt(10);
            int d4 = rand.nextInt(10);
            int d5 = rand.nextInt(10);
            int d6 = rand.nextInt(10);
            int d7 = rand.nextInt(10);
            int d8 = rand.nextInt(10);
            int d9 = rand.nextInt(10);
            int d10 = rand.nextInt(10);
            int d11 = rand.nextInt(10);
     
            sd3 = Integer.toString(d3);
            sd4 = Integer.toString(d4);
            sd5 = Integer.toString(d5);
            sd6 = Integer.toString(d6);
            sd7 = Integer.toString(d7);
            sd8 = Integer.toString(d8);
            sd9 = Integer.toString(d9);
            sd10 = Integer.toString(d10);
            sd11 = Integer.toString(d11);
     
            numRemainder= ""+sd3+""+sd4+""+sd5+""+sd6+""+sd7+""+sd8+""
                    +sd9+""+sd10+""+sd11+"";
     
            numComplete = ""+numStart+""+numRemainder+"";
     
            System.out.println(numComplete);
     
        }
    }


    I would really like some advice on this.

    First of all, the program seems to work but to me it looks really 'beginner like' and unprofessional.

    For example am i converting the integers to strings correctly ? Is there a quicker or more recommended way to do this rather than typing it all out twice.

    Also i don't like the way i am joining the String together as one. the quotation and + marks just to simply join each digit together looks really ametuer(ish). It kind of looks like cheating and unprofessional.

    So are there better, alternative ways i should be:

    - Generating the 9 random digits
    - Converting the Integers to Strings
    - Joining the Strings together to make the final String
    - Or Maybe should the output even be a String ? Maybe it could be outputted as an int ?


    Many thanks for your suggestions and feedback.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Mobile Number Generator

    If you only are going to concatenate the random digits to build a String, you could use a loop and concatenate them one by one to the String in the loop.


    Another idea would be to build the String using the StringBuilder class instead of concatenating the separate numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Mobile Number Generator

    Hi,

    I'm new in Java programming and I have made a generator with your requeriments as exercise. I made it using an array of 9 positions and filling them with random, then converting the array to string.

    Cya

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Mobile Number Generator

    @Darkgore Please don't write code for OPs.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Mobile Number Generator

    Quote Originally Posted by Norm View Post
    @Darkgore Please don't write code for OPs.
    Sorry, I will edit and remove my code from my post

    Thanks

  6. The Following User Says Thank You to Darkgore For This Useful Post:

    Norm (November 28th, 2012)

Similar Threads

  1. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  2. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  3. Replies: 0
    Last Post: January 25th, 2011, 01:24 AM
  4. Fibonacci generator and gives the number back as output
    By aurora in forum Algorithms & Recursion
    Replies: 1
    Last Post: June 20th, 2009, 12:58 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM