-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcho.java
More file actions
65 lines (50 loc) · 2.52 KB
/
Echo.java
File metadata and controls
65 lines (50 loc) · 2.52 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Scanner;
public class Echo{
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user for input
System.out.print("Enter a string (at least three characters long): ");
String userInput = scanner.nextLine();
// Close the scanner to free up resources
scanner.close();
// Check if the input is at least three characters long and the last three characters are letters
if (userInput.length() < 3 || !areLastThreeCharactersLetters(userInput)) {
System.out.println("Invalid input. Please enter a string of at least three characters with letters at the end.");
System.exit(0);; // Terminate the program
}
// Line 1: Repeat the entire input in uppercase letters (initial amplification)
System.out.println(userInput.toUpperCase());
// Line 2: Repeat the last three characters in uppercase letters
System.out.println(userInput.substring(userInput.length() - 3).toUpperCase());
// Line 3: Repeat the last three characters in lowercase letters
System.out.println(userInput.substring(userInput.length() - 3).toLowerCase());
// Line 4: Repeat the last two characters in lowercase letters
System.out.println(userInput.substring(userInput.length() - 2).toLowerCase());
// Line 5: Repeat the last character in lowercase letters
System.out.println(userInput.charAt(userInput.length() - 1));
}
// Method to check if the last three characters of a string are letters
public static boolean areLastThreeCharactersLetters(String str) {
// check if str is null
int length = str.length();
if (length < 3) {
return false;
}
// boolean allLetters = str.chars().allMatch(Character::isLetter);
char char1 = str.charAt(length - 3);
char char2 = str.charAt(length - 2);
char char3 = str.charAt(length - 1);
return Character.isLetter(char1) && Character.isLetter(char2) && Character.isLetter(char3);
}
// public static boolean areLastThreeCharactersLetters(String str) {
// int length = str.length();
// if (length < 3) {
// return false;
// }
// String lastThree = str.substring(str.length()-3 );
// boolean allLetters = str.substring(str.length()-3 ).chars().allMatch(Character::isLetter);
// //
// return allLetters;
// }
}