If 'str' is a String variable with the possibility of it containing white spaces that you need removed, then use the trim() function to attain the desired output/string value:
System.out.println(str.trim());
OR
str_out=str.trim();
System.out.println(str_out);
//if str=" Hello, have a nice day "
//Output:
//Hello, have a nice day
If you're getting into JAVA, be sure to check out this Java course for a smooth learning experience: