Java Program to Convert String to Char Stream Without Using Stream
In this tutorial, we are going to learn how to Convert String to Char Stream without Using Stream. A char Stream is a sequential flow of characters, representing an array of characters where each character occupies a specific position. The task is to convert String to character stream without using Stream. Let's look at a few examples for better understanding.
Example 1:
Input: String = OnlineTutorials
Output: [O, n, l, i, n, e, T, u, t, o, r, i, a, l, s]
Explanation: The input String is converted into an array of characters while maintaining the same order.
Example 2:
Input: String = Conclusion
Output: [C, o, n, c, l, u, s, i, o, n]
Explanation: The array is created by storing each character of the input string individually.
Approach 1: Using charAt() method
The following example explains how to convert a String into a character array. We are creating a String variable named strg. Next, we determine its length and create a character array with the same length. After that, using the charAt() method iterating through each character in the string and adding each character to the corresponding index of the character array. The character array is finally printed as output.
FileName: StringtoCharStreamConversionEx.java
// importing util packages import java.util.*; public class StringtoCharStreamConversionEx { // main method public static void main(String[] args) { // provide input values String strg = "OnlineTutorials"; // initializing length of the string to a variable int len = strg.length(); // character array of string length char[] charArr = new char[len]; // iterating through each character for (int i = 0; i < len; i++) { // assigning the current character to ith index of the char array charArr[i] = strg.charAt(i); } // printing the charArr System.out.println(Arrays.toString(charArr)); } }
Output:
[O, n, l, i, n, e, T, u, t, o, r, i, a, l, s]
Approach 2: Using toCharArray() method
In this example, the String is converted to a character array using the toCharArray() method, and the result is stored in charArr. Finally, we print the character array using Arrays.toString(charArr), which outputs each character in the array.
FileName: StringtoCharStreamConversionEx.java
// importing util packages import java.util.*; public class StringToCharStreamConversionEx { // main method public static void main(String[] args) { // provide input values String strg = "OnlineTutorials"; // character array using toCharArray() method char[] charArr = strg.toCharArray(); // printing the charArr System.out.println(Arrays.toString(charArr)); } }
Output:
[O, n, l, i, n, e, T, u, t, o, r, i, a, l, s]
Approach: Using StringReader Class
In this example, we are using StringReader Class to convert String to character. Using a string, we are creating the StringReader object. Next, we read the characters of string using a while loop, subsequently converting them to characters and inserting them in charArr array. The contents of the charArr array are printed using Arrays.toString(charArr), which produces a string representation of each character.
FileName: StringtoCharStreamConversionEx.java
// importing package and Arrays class import java.io.IOException; import java.io.StringReader; import java.util.Arrays; public class StringToCharStreamConversionExample{ // main method public static void main(String[] args) throws IOException { // provide input values String strg = "OnlineTutorials"; // initializing length of the string to a variable int len = strg.length(); char[] charArr = new char[len]; // object of StringReader class object StringReader sr = new StringReader(strg); int varbl; int currIndx = 0; // reading the characters from the StringReader until the end of the string. while ((varbl = sr.read()) != -1) { charArr[currIndx] = (char)(varbl); currIndx++; } System.out.println(Arrays.toString(charArr)); } }
Output:
[O, n, l, i, n, e, T, u, t, o, r, i, a, l, s]