Basically what the title asks. I have seen multiple methods, such as using Apache commons, and UUID that seem too complicated for someone that just barely started Java.
Asked
Active
Viewed 167 times
1
-
If you are just starting to learn Java, you will not benefit from some code thrown at you. I think you should start with some other problems since you need some basic understanding about objects to unterstand the solution to this problem. – Turing85 Apr 13 '15 at 21:14
-
1Define "absolute simplest", and what is more, define "alphanumeric"; recall for instance that for non English languages, there is more to "alpha" than a to z and even more to numbers than 0 to 9. What is more, what length should the resulting string have? – fge Apr 13 '15 at 21:14
-
Simplest meaning using methods for beginners. In this case it was a .append that was needed, but I wasn't sure how to use it. – AndoTwo Apr 13 '15 at 22:00
1 Answers
2
If really you want to do it yourself then you could do :
Random r = new Random();
String alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
StringBuilder result = new StringBuilder();
final int lengthOfDesiredString = 25;
for (int i = 0; i < lengthOfDesiredString; i++)
result.append(alphabet.charAt(r.nextInt(alphabet.length())));
System.out.println(result);
But IMHO it is way simplier to use library then to re-invent the wheel.

Jean-François Savard
- 20,626
- 7
- 49
- 76
-
Agreed, but my teacher is a finicky one, and wants us to do things the hard way first. – AndoTwo Apr 13 '15 at 21:36
-
@AndoTwo Note that it is a good thing when you are learning to understand how thing are built in java. But for sure, when you have a job, your boss won't like if you spend time coding thing that already exists. All about the money. – Jean-François Savard Apr 13 '15 at 21:38