Java For Each Character In String

Java For Each Character In String | Here in the blog, we will iterate through each character in the string that is we visit for every character in string Java.

Java For Each Char In String

Here, the iteration is done by using for loop. In this code, we search for each String char Java. The algorithm for the below code is as follows:-
Step-1: Declare the string namely, “string”.
Step-2: Define the value for the string.
Step-3: Iterate over the string by using for loop for each character “i” print ” i” .

Java for each character In String Program using for loop

public class Main {
   public static void main(String[] args) {

      String string = "Java Programming Language";
      System.out.println("Characters in " + string + " are:");

      for (int i = 0; i < string.length(); i++) {
         char chr = string.charAt(i);
         System.out.print(chr + ", ");
      }
   }
}

Output:-

Characters in Java Programming Language are:
J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e,

In the output the last character also contains “,”. To remove them from the output screen we can simply use if-else statements as below:-

for (int i = 0; i < string.length(); i++) {
   char chr = string.charAt(i);
   if (i == string.length() - 1) {
      System.out.print(chr);
   } else {
      System.out.print(chr + ", ");
   }
}

Output:-

Characters in Java Programming Language are:
J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e

For Each Character In String Java

The demonstration is done by using the for-each loop. For each loop is advanced for loop instead of declaring, initializing, incrementing, or decrementing a variable we can just declare and use the variable in a for-each loop.
Step-1: Declare the string namely, “string”.
Step-2: Define the value for the string.
Step-3: Iterate over the string by using a for-each loop. For each character “c” print “c”.

Java for each character In String Program using for-each loop

public class Main {
   public static void main(String[] args) {
      String string = "Java Programming Language";
      System.out.println("Characters in " + string + " are:");

      for (char c : string.toCharArray()) {
         System.out.print(c + ", ");
      }
   }
}

Output:-

Characters in Java Programming Language are:
J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e,

Occurrence of Each Character in String Java Using For Loop

To count the occurrence of each character in a string we can take the help of the Map collection. The map will contain character as key and its occurrence as value. Let us demonstrate it through an example:-

import java.util.HashMap;

public class Main {
   public static void main(String[] args) {
      String string = "Java Programming Language";

      HashMap<Character, Integer> occurrence = new HashMap<>();
      for (int i = 0; i < string.length(); i++) {
         char ch = string.charAt(i);
         if (!occurrence.containsKey(ch)) {
            occurrence.put(ch, 1);
         } else {
            occurrence.put(ch, occurrence.get(ch) + 1);
         }
      }
      
      System.out.println("Occurence: " + occurrence);
   }
}

Output:-

Occurence:
{ =2, a=5, e=1, g=4, i=1, J=1, L=1, m=2, n=2, o=1, P=1, r=2, u=1, v=1}

Also see:- Java Program to Count Number of Vowels in a String

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.