3

I want to take a String input form a user, and format it so that the first letter is capitalized and the rest is not. I would like to do this by splitting the first letter from the string and use .toUpperCase() on it and use .toLowerCase() on the rest, and then merge them back together.

I have an idea, but can't solve everything:

userInput = input.nextLine();
String firstLetter = ???
firstLetter.toUpperCase();
restOfString.toLowerCase();
String merged = firstLetter + restOfString;

This does NOT seem to work:

            name = input.nextLine();
            firstLetter = name.substring(0,1);
            remainingString = name.substring(1);
            firstLetter.toUpperCase();
            remainingString.toLowerCase();
            name = firstLetter + remainingString;
Casper TL
  • 318
  • 4
  • 7
  • 16

2 Answers2

6

You can use substring.

String firstLetter = userInput.substring(0,1); //takes first letter
String restOfString = userInput.substring(1); //takes rest of sentence
firstLetter = firstLetter.toUpperCase(); //make sure to set the string, the methods return strings, they don't change the string itself
restOfString = restOfString.toLowerCase();
String merged = firstletter + restOfString;

Edit: If you wish to do error-checking on the user's input:

if(userInput.length < 2) {
    throw new InputMismatchException("Sentence too short to properly capitalize!";
}
Compass
  • 5,867
  • 4
  • 30
  • 42
  • Two error checks: The string needs one letter for `.substring(0, 1)` to work, and it needs two letters for `.substring(1)` to work. Depending on our specific use, it may not matter. – Brian J Oct 23 '14 at 20:02
  • @BrianJ I've added error handling as an aside at the end of my answer. – Compass Oct 23 '14 at 20:05
1

I am guessing you are using Java based on toUpperCase(). What I would suggest you do, is use charAt() to get the first letter, and substring to get the rest.

You can try something like this:

String firstLetter = userInput.substring(0, 1); // Get first element. If you don't understand substring, let me know.
string remainingString = userInput.substring(1); // Grab chars from index 1 to the end.

firstLetter.toUpperCase(); // Capitalize string
remainingString.toLowerCase(); // Lowercase rest of string

String finalString = firstLetter + remainingString;

Hope this helps.

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • Thanks, looks very good! I do get 1 error at the "CharAt(0)" though.. Incompatible types: char cannot be converted to java.lang.string - Do you know why? :) do i have to import any packages to do this? – Casper TL Oct 23 '14 at 20:05
  • @CasperTL no, I think that is my fault. Perhaps char cannot be converted, I will adjust my answer to use substring. – AdamMc331 Oct 23 '14 at 20:11
  • Thanks for your time! i got it to work with the answer by Compass though (via substrings) :) – Casper TL Oct 23 '14 at 20:17
  • Fair enough, answers are the same. First come first serve I suppose! Feel free to ask if you have any other questions. – AdamMc331 Oct 23 '14 at 20:18