TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Random Lowercase Letter

Use the Random class to generate random lowercase letters. Use these chars in random strings.
Random lowercase letters. The letters are "r, p, x, m." They are lowercase. But they are randomly ordered, randomly generated with the Random class.Random
A range. We can represent lowercase ASCII letters with the numbers 0 through 25 inclusive. There are 26 lowercase letters. We turn ints to chars with a cast.ASCII TableCast
Example program. This program uses Random to generate lowercase letters. These chars could be used to create random strings (or for other nefarious purposes).

First: We call nextInt with an exclusive bound of 26. This yields the values 0 through (and including) 25.

Add: We add 97 to the values to adjust to the lowercase characters (97 is the ASCII code for lowercase A).

Caution: This code does not handle international (Unicode) characters. It just handles lowercase ASCII letters. It is limited.

Java program that generates random lowercase letters import java.util.Random; public class Program { public static void main(String[] args) { Random random = new Random(); // Generate 10 random lowercase letters. for (int i = 0; i < 10; i++) { // Max value is exclusive. // ... So this returns 1, 2, through 25. int n = random.nextInt(26); // Add 97 to move from integer to the range A to Z. char value = (char) (n + 97); // Display our results. System.out.println(value + "..." + Integer.toString(n)); } } } Output s...18 o...14 y...24 d...3 t...19 p...15 q...16 f...5 f...5 h...7
Some notes. This algorithm does not support characters with accents (like would be wanted in French). I like French and French is a good language, but this algorithm won't support it.
Other characters, like spaces, could be added to an output stream. We could add the chars to a StringBuilder. This could generate random text.
But for now, the simplest solution is sufficient. We generate lowercase "A" through "Z." The code should be encapsulated in a method.Methods
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf