Ignore the case sensitivity
In this step, you will modify the code to ignore the case sensitivity when checking whether a character is a part of a Java identifier or not.
Add the following code to replace the contents of the main
method in the Main.java
file:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String line = scanner.nextLine().toLowerCase();
for (int i = 0; i < line.length(); i++) {
boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(line.codePointAt(i));
char c = line.charAt(i);
if(isJavaIdentifierPart) {
System.out.println("The character '" + c + "' at position " + i + " is a part of a Java identifier.");
} else {
System.out.println("The character '" + c + "' at position " + i + " is not a part of a Java identifier.");
}
}
scanner.close();
}
The above code converts the user input string to lowercase and then checks whether each character in the string is a part of a Java identifier or not, ignoring the case sensitivity.
To run the code, use the following command in the terminal:
javac Main.java && java Main