How to generate a random alphanumeric string that's limited to 10 character in netbeans, im trying to save an attachment file as a random alphanumeric filename.
Asked
Active
Viewed 792 times
-3
-
Is _netbeans_ an important part of the question or is is just about _Java_? – dotvav Aug 04 '15 at 15:28
-
2you have to show some effort of your own. read about random in java, think about how using it, converting numbers to strings and chars, using string manipulation, ... – hoijui Aug 04 '15 at 15:28
-
If you don't write more then one file per minute, you can use current time as a unique string for your file name `YY MM DD hh mm` – MaxZoom Aug 04 '15 at 15:31
-
You should have tried to look for it at least once before asking it on SO. I wonder how did you miss the related questions prompted while creating this question as well. – Vamshi Krishna Alladi Aug 04 '15 at 15:40
1 Answers
0
Use random and string builder for max efficiency.
Random rand = new Random();
StringBuilder stringBuilder = new StringBuilder("")
char[] chars = {'a','b', ...}
for (int i = 0; i < 10; ++i) {
char selectedChar = chars[rand.nextInt(chars.length)];
stringBuilder.append(selectedChar);
}
String string = stringBuilder.toString();

Anon10W1z
- 167
- 10