Java Program to Convert String to Char Stream Without Using Stream
Last Updated :
02 Feb, 2024
Char stream defines the array of characters. In this article, we will learn the different types of methods for converting a String into a char stream in Java without using Stream . Let us see some methods one by one.
Examples
Input: String = HelloGeeks Output: [H, e, l, l, o, G, e, e, k, s]
Input: String = Introduction Output: [I, n, t, r, o, d, u, c, t, i, o, n]
Methods to Convert String to Char Stream Without Using Stream in Java
We will see 3 methods for converting a string into a character array. Those 3 methods are defined below:
Program to convert string to char stream in Java without using Stream
Below are the implementation of the 3 methods:
1. Using charAt() method
Below is the implementation of converting string to char stream using the charAt() method:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
String str = "HelloGeeks";
int len = str.length();
char[] char_array = new char[len];
for (int i = 0; i < len; i++)
{
char_array[i] = str.charAt(i);
}
System.out.println(Arrays.toString(char_array));
}
}
Output
[H, e, l, l, o, G, e, e, k, s]
Explaination of the above Program:
Take any string.
Declare an array of characters having the same length as the taken string.
Loop over the string and access each character one by one using charAt() method, simultaneously adding that character into i th index of the declared character array.
Output the character array.
2. Using toCharArray() function
Below is the implementation of converting string to char stream using toCharArray() function:
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
String str = "HelloGeeks";
char[] char_array = str.toCharArray();
System.out.println(Arrays.toString(char_array));
}
}
Output
[H, e, l, l, o, G, e, e, k, s]
Explaination of the above Program:
Take any string.
Declare a character array and use toCharArray() method on the string.
toCharArray() method will return an array, assign it to declared char array.
Output character array.
3. Using String Reader Class
String Reader class can also be used to convert a string into character array. This class can be use by importing Java.io package.
Below is the implementation of converting string to char stream using String Reader Class:
Java
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
throws IOException
{
String str = "HelloGeeks";
int len = str.length();
char[] char_array = new char[len];
StringReader reader = new StringReader(str);
int int_var;
int current_index = 0;
while ((int_var = reader.read()) != -1)
{
char_array[current_index] = (char)(int_var);
current_index++;
}
System.out.println(Arrays.toString(char_array));
}
}
Output
[H, e, l, l, o, G, e, e, k, s]
Explaination of the above Program:
import java.io package.
Create an object of StringReader class.
Declare a character array of length equivalent to string.
Declare an integer variable.
Run a while loop and take one by one character into integer variable returned from StringReader object. assign the current character at i th index of character array.
Output character array.
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...