import java.util.Scanner;
import java.util.StringTokenizer;
public class ConsecutiveLetters {
static boolean hasConsecutiveLetter(String word) {
for (int i = 0; i < word.length( ) - 1; i++)
if (word.charAt(i) + 1 == word.charAt(i + 1))
return true;
return false;
}
public static void main(String[ ] args) {
System.out.print("Enter a sentence - ");
for (var st = new StringTokenizer(
new Scanner(System.in).nextLine( )); st.hasMoreTokens( ); ) {
String word = st.nextToken( );
if (hasConsecutiveLetter(word))
System.out.println(word);
It allows us to break a string into tokens or simply put, into words separated by space. It is an easy way to break a String.
Copyright © 2024 EHUB.TIPS team's - All rights reserved.
Answers & Comments
Program
import java.util.Scanner;
import java.util.StringTokenizer;
public class ConsecutiveLetters {
static boolean hasConsecutiveLetter(String word) {
for (int i = 0; i < word.length( ) - 1; i++)
if (word.charAt(i) + 1 == word.charAt(i + 1))
return true;
return false;
}
public static void main(String[ ] args) {
System.out.print("Enter a sentence - ");
for (var st = new StringTokenizer(
new Scanner(System.in).nextLine( )); st.hasMoreTokens( ); ) {
String word = st.nextToken( );
if (hasConsecutiveLetter(word))
System.out.println(word);
}
}
}
StringTokenizer class
It allows us to break a string into tokens or simply put, into words separated by space. It is an easy way to break a String.
Algorithm