Reputation: 519
For some reason when I run the following code the program prints out "JDN4" or "JDN16" when at all times I want it to be printing "JDN" followed by three numbers ranging from 0-9; for example, "JDN123" or "JDN045" or "JDN206". What is wrong with the code?
import java.util.*;
public class Document2 {
public static void main(String[] args) {
String initials = "JDN";
int randomNumbers;
Random generator = new Random();
int randomNumber1 = generator.nextInt(9);
int randomNumber2 = generator.nextInt(9);
int randomNumber3 = generator.nextInt(9);
randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;
String userName = initials + randomNumbers;
System.out.println("Your username is " + userName + ".");
}
}
Upvotes: 1
Views: 3132
Reputation: 691755
What's wrong is that the +
operator does an addition, and not a concatenation when used on int
variables.
Use
String randomNumbers = Integer.toString(randomNumber1) + Integer.toString(randomNumber2) + Integer.toString(randomNumber3);
to convert the numbers into strings and concatenate them.
Upvotes: 1
Reputation: 85468
Your problem is randomNumbers
is declared as an int
, so when you use the +
on the other ints, it will sum the integers rather than concatenate them.
So, if we were to use your approach, we'd need a string:
String userName = initials + randomNumber1 + randomNumber2 + randomNumber3;
But it would be a lot easier just to generate a number from 0-999 and pad it with zeroes:
int randomNumber = generator.nextInt(1000);
String usernName = initials + String.format("%03d", randomNumber);
Something else worth noting is that Random.nextInt(n)
generates a random number between 0 (inclusive) and n
(exclusive), so you probably should be doing .nextInt(10)
if you want a number between 0 and 9.
Upvotes: 6
Reputation: 94645
Try,
String userName=String.format("%s%d%d%d",initials,randomNumber1,randomNumber2,randomNumber3);
Upvotes: 3
Reputation: 81349
You are adding three digits, that would at most give you 27. You want to concatenate the string representation of those digits.
That said, why don't you simply generate a random number between 0 and 999?
Upvotes: 0
Reputation: 5848
You are adding the numbers here.
randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;
If you want to achieve what you are looking for using this same method you could do.
randomNumbers = randomNumber1 + (randomNumber2 * 10) + (randomNumber3 * 100);
Upvotes: 1
Reputation: 223033
Suppose you have three numbers:
int randomNumber1 = 3;
int randomNumber2 = 4;
int randomNumber3 = 5;
int randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;
Do you expect randomNumbers
to be 345? Or do you expect it to be 12?
Now try this:
String randomNumbers = "" + randomNumber1 + randomNumber2 + randomNumber3;
What do you get this time?
Upvotes: 3
Reputation: 3380
randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;
You're adding the numbers, not concatenating them.
Suggested solution: use only 1 random number with
generator.nextInt(1000);
Or, you can go like:
String userName = initials + randomNumer1 + randomNumber2 + randomNumber3;
But I'm nearly sure you'll get 2 or even 3 times the same number, so you should use the first approach instead.
Upvotes: 2