Advertisement
SenpaiZero

Untitled

May 13th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. do {
  8. String word1, word2, sub; // Initialize 3 string varialbes
  9. Scanner scanner = new Scanner(System.in); // Initialize Scanner
  10.  
  11. // Get the user input as value word1
  12. System.out.print("Enter the first word: ");
  13. word1 = scanner.nextLine();
  14. sub = word1.substring(word1.length()-2); // get the last 2 letters
  15.  
  16. // Get the user input as value word2
  17. System.out.print("Enter the second word: ");
  18. word2 = scanner.nextLine();
  19.  
  20. // Initialize pattern and matcher
  21. Pattern pattern = Pattern.compile("[a-zA-Z]{1,2}" + sub); // Allows small and capital letter {1,2} means 1 or 2 characters
  22. Matcher matcher = pattern.matcher(word2);
  23.  
  24. // Check if matcher is true
  25. if(matcher.matches()) {
  26. System.out.println(word2 + " rhymes " + word1);
  27. }
  28. else {
  29. System.out.println("I'm not sure! Sorry!");
  30. System.exit(0); // Stop the program
  31. }
  32.  
  33. System.out.println("\n");
  34. } while(true);
  35. }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement