
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Print even length words
In this article, we will understand how to print even length words. String is a datatype that contains one or more characters and is enclosed in double quotes (“ ”). Char is a datatype that contains an alphabets or an integer or an special character.
Below is a demonstration of the same −
Suppose our input is −
Input string: Java Programming are cool
The desired output would be −
The words with even lengths are: Java cool
Algorithm
Step 1 - START Step 2 - Declare a string namely input_string. Step 3 - Define the values. Step 4 - Iterate over the string usinf a for-loop, compute word.length() modulus of 2 for each word to check if the length gets completely divided by 2. Store the words. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class EvenLengths { public static void main(String[] args) { String input_string = "Java Programming are cool"; System.out.println("The string is defined as: " +input_string); System.out.println("\nThe words with even lengths are: "); for (String word : input_string.split(" ")) if (word.length() % 2 == 0) System.out.println(word); } }
Output
The string is defined as: Java Programming are cool The words with even lengths are: Java cool
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class EvenLengths { public static void printWords(String input_string) { System.out.println("\nThe words with even lengths are: "); for (String word : input_string.split(" ")) if (word.length() % 2 == 0) System.out.println(word); } public static void main(String[] args) { String input_string = "Java Programming are cool"; System.out.println("The string is defined as: " +input_string); printWords(input_string); } }
Output
The string is defined as: Java Programming are cool The words with even lengths are: Java cool
- Related Articles
- Python program to print even length words in a string
- Java Program to Print all unique words of a String
- Java program to print odd and even number from a Java array.
- Java program to Print Odd and Even Number from an Array
- Reversing the even length words of a string in JavaScript
- Python program to print Possible Words using given characters
- C++ program to print unique words in a file
- Python program to print even numbers in a list
- Python program to remove K length words in String
- Python program to print all even numbers in a range
- C++ Program to Print “Even” or “Odd” without using conditional statement
- C Program to print “Even” or “Odd” without using Conditional statement
- Print Even Positions Elements from an Array in Java
- C# Program to Print the Length of the Hashtable
- Program to find maximum length of non-sharing words in Python

Advertisements