First time posting, so if I have formatting problems I'm sorry

 int randNum=0;
     Scanner in= new Scanner (System.in);
     Random randNumlist= new Random();
     System.out.println("lowercase letters [a]");
      String input= in.next();
      System.out.println(" how many letters max 14");
      int input2=in.nextInt();
     if ( input.equalsIgnoreCase("A"))
     while( !(randNum>= (char)97 && randNum<= (char)122))
         for (int n=1; n <=input2; n++)
             randNum= randNumlist.nextInt(135);


I'm trying to generate all lower case characters to create a password with ASCII. But it won't generate the numbers I want.

3 Years
Discussion Span
Last Post by JamesCherrill

Hi, welcome to DaniWeb.
Lower case letters are 26 consecutive Unicode values starting with 'a', so the cleanest way to generate a random lower case letter is to get a random integer in the range 0-25 and add it to 'a', as in

'a' + randNumlist.nextInt(26)

But, that calculation will be performed in int (32 bits), up-converting the char 'a' to int, so you will need to cast it back to char (16 bits) at th end...

(char) ('a' + randNumlist.nextInt(26))

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.