Let's get started with a Microservice Architecture with Spring Cloud:
Increment Character in Java
Last updated: January 8, 2024
1. Overview
In this tutorial, we’ll learn how to generate a sequence of characters from ‘A’ to ‘Z’ in Java. We’ll do this by incrementing ASCII values.
In Java, we represent the ASCII values in Unicode since there is an extended range of ASCII characters. The standard ASCII table is limited to 127 characters. Unicode encompasses many more characters than ASCII, allowing for internationalization and the use of diverse symbols. Thus in Java we are not limited to just the standard values of ASCII.
We’ll be generating a sequence of characters using a for loop and IntStream from the Java 8 Stream API.
2. Using a for Loop
We’ll create a list of capital letters from ‘A‘ to ‘Z‘ using a standard for loop:
@Test
void whenUsingForLoop_thenGenerateCharacters(){
List<Character> allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
List<Character> characters = new ArrayList<>();
for (char character = 'A'; character <= 'Z'; character++) {
characters.add(character);
}
Assertions.assertEquals(alphabets, allCapitalCharacters);
}
Every letter has a unique number in the ASCII system. For example, ‘A’ is represented as 65, ‘B’ as 66 continuing to the end ‘Z’ as 90.
In the example above, we increment the char type and add it to a list in a for loop.
Finally, by using the assertEquals() method of the Assertions class, we check if the generated list matches the expected list of all capital characters.
3. Using Java 8 IntStream
Using Java 8 IntStream, we can generate a sequence of all capital letters from ‘A’ to ‘Z’:
@Test
void whenUsingStreams_thenGenerateCharacters() {
List<Character> allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
List<Character> characters = IntStream.rangeClosed('A', 'Z')
.mapToObj(c -> (char) c)
.collect(Collectors.toList());
Assertions.assertEquals(characters, allCapitalCharacters);
}
In the above example, employing IntStream from Java 8, we generate characters ranging from ‘A’ to ‘Z’ with ASCII values spanning 65 to 90.
Initially, we map these values to characters and then subsequently collect them into a list.
The IntStream receives as argument two integers, but by passing a char as an argument the compiler will automatically cast it to an integer. If we convert this: IntStream.rangeClosed('A', 'Z') into a list of integers, we will see that there is a list of Integers from 65 to 90.
To conclude, we employ the assertEquals() method from the Assertions class to verify if the generated list aligns with the expected list of all capital letters.
4. Conclusion
In this short article, we explored how we can use the Stream API and for loop to increment the ASCII values of the characters and print their corresponding values.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















