7

I have created a java gwt application in which I want to verify user's email address from client side, is there any way to generate random 5 character code on client side?

Any sort of help will be appreciated.

Cataclysm
  • 7,592
  • 21
  • 74
  • 123
Mahesh More
  • 123
  • 1
  • 6

3 Answers3

6

Something like this?

StringBuilder sb = new StringBuilder();
Random random = new Random();

for (int i=0;i<5;i++) {
    sb.append('a'+random.nextInt(26));
}
String code = sb.toString();
Tim B
  • 40,716
  • 16
  • 83
  • 128
2

Why don't you test with Java Math.random() .You can simply get by it.

Here is useful formula for generating random numbers

(int)(Math.random() * (max - min) + min)

So , you can generate 5 random numbers as like...

String randomCodes = String.valueOf((int) (Math.random() * (99999 - 1) + 1));
    while (randomCodes.length() < 5) {
            randomCodes = "0" + randomCodes;
        }
Cataclysm
  • 7,592
  • 21
  • 74
  • 123
0

You can use the RandomStringUtils from the Apache Commons project,

RandomStringUtils.randomAlphabetic(5);

Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
  • 2
    This will lead me to add an extra library which I don't want. Looking for some that Gwt provide on its own. Else the solutions provided by Tim is good just iteration would be the problem. – Mahesh More Dec 30 '13 at 11:37