Wednesday, July 3, 2024
HomeLanguagesJavaProgram to convert String to IntStream in Java

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:




// 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);
    }
}


Output:

String: Geeks
71
101
101
107
115
Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments