Remove whitespaces and capitalize user input

Viewed 3145

I've made the code so it asks the user various questions, and if the input.trim().isEmpty() a message will be given to the user and will ask the user to input again. So if the user just writes blank spaces, message given. If the user gives a few blank spaces and some characters, it will accept.

Problem right now is that I want to capitalize the first letter of the Word, but it doesn't really work. Say if the user's input start with a letter then that will be capitalized. But if there's whitespace it wont capitalize at all.

So if input is:

    katka

Output is:

katka

Another example:

katka

Output is:

Katka

Code is:

String askWork = input.nextLine();

String workplace = askWork.trim().substring(0,1).toUpperCase()
 + askWork.substring(1);

while (askWork.trim().isEmpty()){ String askWork = input.nextLine();

String workplace = askWork.trim().substring(0,1).toUpperCase()
 + askWork.substring(1);

}

I've tried different approaches but no success.

5 Answers
Related