Java Program to Convert String to Char Stream Without Using Stream
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 = HelloBeginner
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
// Common approah to convert String into character array import java.util.*; // Driver Class public class GFG { // Driver Function public static void main(String[] args) { // Input string String str = "HelloBeginner" ; // Length of string int len = str.length(); // Character array of string length char [] char_array = new char [len]; // Looping and accessing each character for ( int i = 0 ; i < len; i++) { // assigning the current character to ith index // of char array char_array[i] = str.charAt(i); } // printing the char_array System.out.println(Arrays.toString(char_array)); } } |
[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 ith 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
// Converting String into character array using toCharArray() method import java.util.*; // Driver Class public class Main { // Driver Function public static void main(String[] args) { // Input string String str = "HelloBeginner" ; // Character array using toCharArray() method char [] char_array = str.toCharArray(); // printing the char_array System.out.println(Arrays.toString(char_array)); } } |
[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
// Converting String to Character array using StringReader class // importing package and Arrays class import java.io.IOException; import java.io.StringReader; import java.util.Arrays; // Driver class public class GFG { // Driver function public static void main(String[] args) throws IOException { // String String str = "HelloBeginner" ; // length of string int len = str.length(); // character array char [] char_array = new char [len]; // StringReader class object StringReader reader = new StringReader(str); // integer variable int int_var; int current_index = 0 ; // taking current character into integer variable // from StringReader object until returned variable // is not equal to -1 (-1 denotes end of string) while ((int_var = reader.read()) != - 1 ) { char_array[current_index] = ( char )(int_var); current_index++; } System.out.println(Arrays.toString(char_array)); } } |
[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 ith index of character array.
- Output character array.
Contact Us