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.

# Thread: Mobile Number Generator

1. ## 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. ## 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.

3. ## 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. ## Re: Mobile Number Generator

@Darkgore Please don't write code for OPs.

5. ## Re: Mobile Number Generator

Originally Posted by Norm
@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)