Program to convert String to IntStream in Java

Given a String, the task is to convert this String into an IntStream containing the ASCII values of the characters as the elements.

Examples:

Input: String = Geeks
Output: 71, 101, 101, 107, 115

Input: String = GeeksForGeeks
Output: 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115

Algorithm:

  1. Get the String to be converted.
  2. Convert it into IntStream using chars() method.
  3. Print the formed IntStream.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert
// String to IntStream
  
import java.util.stream.IntStream;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Get the String to be converted
        String string = "Geeks";
  
        // Print the String
        System.out.println("String: " + string);
  
        // Convert String to IntStream using chars() method
        IntStream intStream = string.chars();
  
        // Print the elements of the IntStream
        intStream.forEach(System.out::println);
    }
}

chevron_right


Output:

String: Geeks
71
101
101
107
115

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Content Manager at LambdaTest

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.