w3resource

Java: Check two consecutive, identical letters in a given string

Java String: Exercise-108 with Solution

Write a Java program to check whether there are two consecutive (following each other continuously), identical letters in a given string.

Visual Presentation:

Java String Exercises: Check two consecutive, identical letters in a given string
Java String Exercises: Check two consecutive, identical letters in a given string

Sample Solution-1:

Java Code:

public class test {
    public static void main(String[] args) {
        String text = "Follow"; // Define a string 'text' with the value "Follow"
        System.out.println("Original word: " + text); // Display the original word
        
        // Check if there are two consecutive identical letters in the string and display the result
        System.out.println("If there are two consecutive identical letters in the said string: " + test(text));
        
        text = "Java"; // Change the value of 'text' to "Java"
        System.out.println("Original word: " + text); // Display the original word
        
        // Check if there are two consecutive identical letters in the string and display the result
        System.out.println("If there are two consecutive identical letters in the said string: " + test(text));
        
        text = "Yellow"; // Change the value of 'text' to "Yellow"
        System.out.println("Original word: " + text); // Display the original word
        
        // Check if there are two consecutive identical letters in the string and display the result
        System.out.println("If there are two consecutive identical letters in the said string: " + test(text));
    }

    // Method to test if there are two consecutive identical letters in the string
    public static boolean test(String text) {
        for (int i = 0; i < text.length() - 1; i++) {
            if (text.charAt(i) == text.charAt(i + 1)) { // Check for consecutive identical letters
                return true; // Return true if two consecutive identical letters are found
            }
        }
        return false; // Return false if no consecutive identical letters are found
    }
}

Sample Output:

Original word: Follow
If there are two consecutive identical letters in the said string: true
Original word: Java
If there are two consecutive identical letters in the said string: false
Original word: Yellow
If there are two consecutive identical letters in the said string: true

Flowchart:

Flowchart: Java String Exercises - Count Occurrences Of Character

Sample Solution-2:

Java Code:

import java.util.stream.IntStream;

public class test {

    public static void main(String[] args) {
        String text = "Follow"; // Define a string 'text' with the value "Follow"
        System.out.println("Original word: " + text); // Display the original word
        
        // Check if there are two consecutive identical letters in the string and display the result
        System.out.println("If there are two consecutive identical letters in the said string: " + test(text));
        
        text = "Java"; // Change the value of 'text' to "Java"
        System.out.println("Original word: " + text); // Display the original word
        
        // Check if there are two consecutive identical letters in the string and display the result
        System.out.println("If there are two consecutive identical letters in the said string: " + test(text));
        
        text = "Yellow"; // Change the value of 'text' to "Yellow"
        System.out.println("Original word: " + text); // Display the original word
        
        // Check if there are two consecutive identical letters in the string and display the result
        System.out.println("If there are two consecutive identical letters in the said string: " + test(text));
    }

    // Method to test if there are two consecutive identical letters in the string using IntStream
    public static boolean test(String text) {
        return IntStream.range(0, text.length() - 1) // Generate a stream of integers from 0 to text.length()-1
                .anyMatch(n -> text.charAt(n) == text.charAt(n + 1)); // Check if any pair of consecutive letters are identical
    }
}

Sample Output:

Original word: Follow
If there are two consecutive identical letters in the said string: true
Original word: Java
If there are two consecutive identical letters in the said string: false
Original word: Yellow
If there are two consecutive identical letters in the said string: true

Flowchart:

Flowchart: Java String Exercises - Count Occurrences Of Character

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous Java Exercise: Counts occurrences of a certain character in a string.
Next Java Exercise: Reverses the words in a string that have odd lengths.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.