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
// 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 = "HelloGeeks";
// 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));
}
}
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 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 = "HelloGeeks";
// Character array using toCharArray() method
char[] char_array = str.toCharArray();
// printing the char_array
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
// 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 = "HelloGeeks";
// 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));
}
}
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 ith index of character array.
- Output character array.
Similar Reads
How to Convert String to Char Stream in Java without Using Library Functions?
In Java, we can convert a string to a char stream with a simple built-in method .toCharArray(). Here, we want to convert the string to a char stream without using built-in library functions. So, we will create an empty char array iterate it with string characters, and store the individual characters
2 min read
Convert String to Stream of Chars in Java
The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil
2 min read
Java Program to Convert OutputStream to String
OutputStream is an abstract class that is available in the java.io package. As it is an abstract class in order to use its functionality we can use its subclasses. Some subclasses are FileOutputStream, ByteArrayOutputStream, ObjectOutputStream etc. And a String is nothing but a sequence of character
2 min read
Program to Convert Stream to an Array in Java
A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition
3 min read
Java Program to Convert a Float value to String
Given a Float value in Java, the task is to write a Java program to convert this float value to string type. Examples: Input: 1.0 Output: "1.0" Input: 3.14 Output: "3.14" Strings - Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are
4 min read
Java Program to Convert String to Byte Array Using getBytes() Method
In Java, strings are objects that are backed internally by a char array. So to convert a string to a byte array, we need a getByte() method. It is the easiest way to convert a string to a byte array. This method converts the given string to a sequence of bytes using the platform's default charset an
2 min read
Program to convert a Map to a Stream in Java
A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Below are various method to convert Map to Stream in Java: Converting complete Map<Key, Value> into Stream: This can be done with the help of Map.entrySet() method which return
4 min read
Java Program to Read a File to String
There are multiple ways of writing and reading a text file. This is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Given a text file, the task is to read the contents
8 min read
Program to convert Boxed Array to Stream in Java
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case
3 min read
Java Program to Convert String to Long
To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a
3 min read