-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringEndsWith.java
More file actions
40 lines (31 loc) · 1.14 KB
/
StringEndsWith.java
File metadata and controls
40 lines (31 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
/**
* 28.Program to check if a string ends with a specific substring in Java.
*/
public class StringEndsWith {
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
String inputStr, valEndStr;
while (true) {
System.out.print("Enter the main string: ");
inputStr = scan.nextLine().trim();
System.out.print("Enter the substring to check: ");
valEndStr = scan.nextLine().trim();
if (inputStr.isEmpty() || valEndStr.isEmpty()) {
System.out.println("Error: Input cannot be empty. Please try again.");
continue;
}
if (!inputStr.matches("[a-zA-Z ]+") || !valEndStr.matches("[a-zA-Z ]+")) {
System.out.println("Error: Input must contain only letters and spaces. Please try again.");
continue;
}
break; // Valid input, exit loop
}
// Checking if the input string ends with the specified substring
boolean result = inputStr.endsWith(valEndStr);
System.out.println("Does the string end with '" + valEndStr + "'?\n" + result);
} catch (Exception e) {
System.out.println("Unexpected error occurred: " + e.getMessage());
}
}
}