Java Program to Find Even Length Words in a String

Learn how to find even Length Words in a String using java. here the split() method is a built-in method of the String class. It divides a string into an array of substrings based on the specified delimiter.

Code Explanation (Step-by-Step)

  • Create a string containing a sentence.
  • Use the split(” “) method to divide the sentence into words.
  • Traverse each word using a for loop .
  • Check whether the length of each word is even by using the modulo (%) operator.
  • If word.length() % 2 == 0, so number is even.
  • Now print the even word sentence.
				
					public class EvenString {
	public static void main(String[] args) {

		String str = "i am a developer";
		String[] words = str.split(" ");
		System.out.println("Even Words String are :- ");
		for (String word : words) {
			if (word.length() % 2 == 0) {
				System.out.println(word);
			}
		}
	}
}
				
			

Output :-
Even Words String are :-
am

What is an Even Length Word in Java 8

				
					import java.util.Arrays;

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

        String str = "Java programming is easy to learn";

        Arrays.stream(str.split(" "))
              .filter(word -> word.length() % 2 == 0)
              .forEach(System.out::println);
    }
}