w3resource

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


108. Check Consecutive Identical Letters

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



For more Practice: Solve these Related Problems:

  • Write a Java program to determine if a string contains any pair of adjacent identical letters.
  • Write a Java program to verify the presence of consecutive duplicate characters in a string.
  • Write a Java program to check for immediate repetition of any character in a given string.
  • Write a Java program to scan a string and return true if any two consecutive characters are the same.

Go to:


PREV : Count Character Occurrences.
NEXT : Reverse Odd-Length Words.

Java Code Editor:

Improve this sample solution and post your code through Disqus

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.