1

I have a really silly question but I don't know what do since I'm a beginner. From the title, you can probably guess what I need help with...

Here is the basic program I have created:

    // Character name.
import java.util.Scanner;

public class name {
    public void charName(){
        Scanner input = new Scanner(System.in);
        String name;
        System.out.println("What is your name?");
        name = input.nextLine();

        System.out.println(name + "? That's a very nice name, pleased to meet you!");
    }
}

This asks for a name, and then repeats the name again along with a statement. My problem is that this replies with the exact thing I have inputted, so if I enter "jake" it will reply with "jake" or if I enter "JaKe" it will reply with "JaKe". I want this program to be able to say persons name with the first letter capitalised no matter how I type it. E.g. I type "JAKE" but it responds with "Jake".

Smarzey
  • 11
  • 4
  • 1
    When people ask to "show your code" - they don't mean "show all of the code from your program, but nothing about your problem". In this case, you have shown nothing that even *tries* to convert the case of your string - everything you've shown is irrelevant. – Jonathon Reinhart Oct 04 '13 at 22:10
  • You can use [WordUtils#capitalizeFully()](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/WordUtils.html#capitalizeFully(java.lang.String)) – nachokk Oct 04 '13 at 22:13

6 Answers6

3

You'll need some methods of the String class:

  • substring(int, int): Get a substring from and to indices
  • substring(int): Get a substring from an index to the end of the string
  • toLowerCase(): Convert the entire string to lowercase
  • toUpperCase(): Convert the entire string to UPPERCASE

Use both substring methods to get the first letter and the rest of the name as separate strings. Then use toUpperCase() and toLowerCase() and concatenate the strings back together.

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

One row:

    String str = "jake";

    String out = str.substring(0, 1).toUpperCase() + str.substring(1);

BTW, as other option you would use WordUtils

WordUtils.capitalize("i am FINE") = "I Am FINE"

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

First lower case the input and then upper case the first char:

String name = "Jake";
String lower= name.toLowerCase();

After that have a look here: First char to upper case

Community
  • 1
  • 1
TerenceJackson
  • 1,776
  • 15
  • 24
0

Does this help:

String capSmall = "jAKE";
String firstLetter = String.valueOf(capSmall.charAt(0));
String theRest = capSmall.substring(1);
String result = firstLetter.toUpperCase() + theRest.toLowerCase();
System.out.println("---"+result);
Tony
  • 345
  • 1
  • 4
  • 11
0

The Apache Commons library has a nice set of string utilities.

The capitalizeFully method does exactly what you need.

WordUtils.capitalizeFully("pOkEmOn lolz NOOBZ"); // returns Pokemon Lolz Noobz

It's true that this solution requires a whole additional dependency but the method handles both single- and multi-word input. Besides, there's a lot of other neat stuff in the library and I literally haven't worked on a project that wouldn't use it.

If you want to stick with vanilla Java API, you can just use

name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();

This will only work on a single word, though. Some users may be tempted to enter full names like Bender Bending Rodriguez. This can be solved easily by combining this with the split method but I'm leaving this to you ;)

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
0

If you can use 3rd party lib. Use this WordUtils#capitalizeFully(String). Or if this is homework and you have to do by yourself see String#toLowerCase() and then String#toUpperCase() for the first character, see String#substring.

nachokk
  • 14,363
  • 4
  • 24
  • 53