Commonly Used Special Cases in JAVA
Commonly Used Special Cases in JAVA
1. Case-insensitive comparison:
String text1 = "hello";
String text2 = "HeLLo";
The `trim()` method removes any leading and trailing white spaces from a string. In this
example, it will print:
The `length()` method returns the number of characters in a string. In this example, it
will print "The string has 5 or fewer characters." because the length of `input` is 5.
try {
int number = Integer.parseInt(numberStr);
System.out.println("Parsed number: " + number);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number format.");
}
The `parseInt()` method parses a string into an integer. In this example, it will print
"Parsed number: 42" because `numberStr` contains the valid integer "42". If
`numberStr` were "abc" instead, it would catch a `NumberFormatException` and print
"Error: Invalid number format."