New at coding, and I'm running some basic exercises to get used to the language. In this exercise I'm trying to generate a phone number with the following restrictions:
I've seen suggestions to add an empty string (which I have), but I don't understand why that works. For now, I'll be sticking with the following, even though I don't fully understand why it works.
num1 = rand.nextInt(7) + 1;
num2 = rand.nextInt(7) + 1;
num3 = rand.nextInt(7) + 1;
num4 = rand.nextInt(643) + 100;
num5 = rand.nextInt(1001) + 1000;
String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5;
System.out.print("Your assigned phone number is: " + number);
EDIT: NEW CODE INCLUDES sb.append
StringBuilder sb = new StringBuilder();
int num1, num2, num3, num4, num5;
num1 = rand.nextInt(7) + 1;
num2 = rand.nextInt(7) + 1;
num3 = rand.nextInt(7) + 1;
num4 = rand.nextInt(643) + 100;
num5 = rand.nextInt(1001) + 1000;
sb.append(num1);
sb.append(num2);
sb.append(num3);
sb.append("-");
sb.append(num4);
sb.append("-");
sb.append(num5);
//String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5;
System.out.print("Your assigned phone number is: " + sb.toString());
@Serge 's answer worked for me. Though it does seem to require more coding with all the sb.append calls I have to include. I've yet to learn about the StringBuilder class, but it definitely seems to be helpful. Thank you, everyone.
Use a stringbuilder
StringBuilder sb = new StringBuilder();
sb.append(num1);
sb.append(num2);
sb.append(num3);
sb.append("-");
sb.append(num4);
sb.append("-");
sb.append(num5);
System.out.println("Your phone number is: " + sb.toString());
here's the explanation:
num1 = rand.nextInt(7) + 1; //This generates a random number between 1 and 7 (because rand.nextInt(n); returns a random number between 0 and n, then first number in a phone number can't be zero, that's why +1 is added.
num4 = rand.nextInt(643) + 100; // This one generates a random number between 1 and 643 and add 100 (because it can be 0 - 99 but that don't give us a number of 3 digits), so we add 100 and it will give us a number of 3 digits.
num5 = rand.nextInt(1001) + 1000; // returns a random number of 4 digits between 1000 and 1001, so basicaly: 1000 or 1001.
String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5; //this will be the example:
number = "367-783-1001";
System.out.println("Your assigned phone number is: " + number);
Your assigned phone number is: 367-783-1001
It concatenates them because we're adding them into a String (text variable) instead of int (number variable)
The way they're adding the numbers is correct
Yeah if you add "" to the beggining the compiler will parse from int to String (I don't know why), but you can change it to:
String number = String.valueOf(num1) + String.valueOf(num2) + String.valueOf(num3) + "-" + String.valueOf(num4) + "-" + String.valueOf(num5);
Or:
String number = Integer.toString(num1) + Integer.toString(num2) + Integer.toString(num3) + "-" + Integer.toString(num4) + "-" + Integer.toString(num5);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With