Find Double Letter Sequence Words in Java – Best 2 Programs

In this post, we will learn how to develop a program to find double letter sequence words in Java. We will use the split( ) method and some simple logic to find the double letter sequence words.

What is Double Letter Sequence Words?

A double letter sequence word is the word that contains the same letter in 2 consecutive places of a word. In this scenario, we will find those double letter sequence words from given sentence and we will display those words to user.

Let’s see now how to find Double Letter sequence words in a sentence in Java. We have used the most simple logic below.

Simple Logic (How to Think to Get the Logic?)

As we want to find double letter sequence words, so our first question is how to find the words of a sentence? Let’s think about how we can find each word from a sentence. By using the split( ) method, we can split any sentence, right?

Yes, absolutely right! We will use the split( ) method to find each word in a sentence.

Ok, we will find all the words. Now, our next question is How to get one by one word?

Simply, if we will use the iteration method like for loop, then easily we can find all words one by one. Yes, correct. We will use for loop to find all words one by one.

We have found each word one by one. Now, the next question is How we can check double letter sequence letters are present on the particular word or not?

If we can compare one letter of each word with the next letter of the same word and if we will find both letters are the same then we can display that particular word to the user. Yes, right. We will use one if condition to compare each letter of words with the next letter of that word and when we will find the next letter is the same as the previous one then we will display that word.

Yes, that’s it friends. We will use these simple logics to find double letter sequence words in Java. Now, Let’s see the complete programs now.

You can also check how to remove a particular character from a String and How to remove duplicate words in a String.

Program-1: Program to Find Double Letter Sequence Words in Java

/** * Program to find double letter sequence words in Java. */ package cf.java.logical; import java.util.Scanner; public class FindDoubleLetterSequenceWord { public static void main(String[] args) { // Declare all variables String sentence, words[]; // Use Scanner class to accept any sentence from user Scanner sc = new Scanner(System.in); System.out.print("Enter any sentence: "); sentence = sc.nextLine().toLowerCase(); // Find all words from given sentence words = sentence.split(" "); System.out.print("Double Letter Sequence Words are: "); // Use for loop to transverse all words of sentence for(int i = 0; i < words.length; i++) { String temp = words[i]; // Use for loop to find double letter sequence words for(int j = 0; j < temp.length() - 1; j++) { // Check for sequence letter is same or not if(temp.charAt(j) == temp.charAt(j + 1)) System.out.print(temp + " "); } } } }
Code language: PHP (php)

Output:

Enter any sentence: She is feeding the little rabbit an apple Double Letter Sequence Words are: feeding little rabbit apple
Enter any sentence: Find double letter sequence words in Java Double Letter Sequence Words are: letter

Program-2: Program to Count Double Letter Sequence Words in Java

In the first program, we have learned how to find double letter sequence words in Java, and in this program, we will learn how to find the count or no of occurrences of double letter sequence letter words.

The logic for this program will be as similar to the previous program logic also, but only in the case of display a particular word, we will count no of double letter sequence words here.

Let’s see the program now.

/** * Program to Count Double Letter Sequence Words in Java */ package cf.java.logical; import java.util.Scanner; public class CountDoubleLetterSequenceWords { public static void main(String[] args) { String sentence, words[]; int count = 0; boolean check = false; // Use Scanner class to accept any sentence from user Scanner sc = new Scanner(System.in); System.out.print("Enter any sentence: "); sentence = sc.nextLine().toLowerCase(); // Find all words from given sentence words = sentence.split(" "); // Use for loop to transverse all words of sentence for(int i = 0; i < words.length; i++) { String temp = words[i]; // Use for loop to find double letter sequence words for(int j = 0; j < temp.length() - 1; j++) { // Check for sequence letter is same or not if(temp.charAt(j) == temp.charAt(j + 1)) { count++; check = true; } } } if(check == true) System.out.println("No of Double Letter Sequence Words is " + count); else System.out.println("No Double Letter Sequence Words Found in The Given Sentence!"); } }
Code language: JavaScript (javascript)

Output:

Enter any sentence: She is feeding the little rabbit an apple No of Double Letter Sequence Words is 4
Enter any sentence: Count double letter sequence words in Java No of Double Letter Sequence Words is 1
Enter any sentence: Learn and grow with Codingface No Double Letter Sequence Words Found in The Given Sentence!
Code language: JavaScript (javascript)

Other Queries Covered in This Post

  • Java Search for word that contain double letter.
  • How to find Double Letter Sequences in a String Program?
  • Write a program that counts double letters.
  • How to Count Consecutive Same Characters in Java?

FAQs

What is Double Letter Sequence Word?

A double letter sequence word is the word that contains the same letter in 2 consecutive places of a word.

How to Count Double Letter Sequence Words?

We can count no of Double Letter Sequence words by using a counter variable. When we will find consecutive same letters, we will increase the count value.

How to Check Consecutive Letters are Same or Not?

We can use if condition to check consecutive letters are the same or not.
Example:
if(word.charAt(j) == word.charAt(j + 1))

1 thought on “Find Double Letter Sequence Words in Java – Best 2 Programs”

Leave a Comment