User input often arrives with extra spaces at the beginning or end. The .trim() method removes leading and trailing whitespace from a string.
String input = " hello ";
String cleaned = input.trim();
System.out.println("[" + cleaned + "]");
This prints [hello]. The spaces inside the string are untouched. Only whitespace at the start and end is removed.
Java introduced .strip(), which does the same thing but handles Unicode whitespace characters that .trim() misses. If your program processes text from international sources, .strip() is the safer choice. There are also .stripLeading() and .stripTrailing() variants that remove whitespace from only one side.
If you skip trimming user input before comparing it, "hello" and "hello " will not be equal, and your comparisons will fail silently.