user1179374
user1179374

Reputation:

Capitalizing the first character of a string - Java

I've looked all over for how to capitalize the first character of a string, but nothing I've found has helped. For my method to work, I need to set a user entered string to lower case.

sourceText = enterText.getText();
char chr = sourceText.charAt(0);

so I have a boolean that's true if the first character is uppercase.

boolean upperCase = Character.isUpperCase(chr);
sourceTextLower = sourceText.toLowerCase();

Cool stuff happens here, and the final product is another string called translatedTextString and an if statement

String s2 = "";
if(upperCase == true)
{
    int x = translatedTextString.length();
    s2 = translatedTextString.substring(0,1).toUpperCase().concat(translatedTextString.substring(1, x));
}

//translatedText is a label
translatedText.setText(s2);

However, when I run the program, the first character of my result is still lower case. So my questions is: is this even the right way to go about doing this? If so, what am I doing wrong, and if not, how can I do it correctly?

Upvotes: 2

Views: 10235

Answers (6)

abdelrahman elattar
abdelrahman elattar

Reputation: 41

You can check this complete code

import java.util.*;
import java.lang.*;
import java.io.*;

public class CapitalizeFirstCharacter
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner input = new Scanner(System.in);
        String word = input.next();

        StringBuilder wordTemp = new StringBuilder(word);
        char firstCharacter = wordTemp.charAt(0);

        if(firstCharacter>=97 && firstCharacter<=122)
        {
            firstCharacter -= 32;
            wordTemp.setCharAt(0, firstCharacter);
        }

        System.out.println(wordTemp.toString());
    }
}

Upvotes: 0

duggu
duggu

Reputation: 38409

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html

or

String source = "hello good old world";
StringBuilder res = new StringBuilder();

String[] strArr = source.split(" ");
for (String str : strArr) {
    char[] stringArray = str.trim().toCharArray();
    stringArray[0] = Character.toUpperCase(stringArray[0]);
    str = new String(stringArray);

    res.append(str).append(" ");
}

System.out.print("Result: " + res.toString().trim());

Upvotes: 0

Lucas
Lucas

Reputation: 5069

If i understand your code correctly, you're changing it to uppercase, when upperCase = true? So the lower case ones wont trigger that flag, and you wont make anything upperCase...

should be

if(!upperCase)

Upvotes: 1

Vinit ...
Vinit ...

Reputation: 1459

Try this complete code:

import java.io.DataInputStream;
import java.io.IOException;

public class captalizeCharactor {
public static void main(String[] args) throws IOException {
    int a1, a2;
    char ch1, ch2;
    String str;
    StringBuilder sb = null;

    DataInputStream in = new DataInputStream(System.in);
    str = in.readLine();
    ch1 = str.charAt(0);
    sb = new StringBuilder(str);
    a1 = ch1;
    if(a1>=97 || a1<=122){
        a2 = a1 - 97;
        ch2 = (char) (65+a2) ;
        sb.setCharAt(0, ch2);
        System.out.println(sb.toString());
    }
}
}

Upvotes: 0

mikera
mikera

Reputation: 106351

I'd just do it as follows, using the Character.toUpperCase function on the first character of the string:

String s ="hello world";
String capitalized = Character.toUpperCase(s.charAt(0)) + s.substring(1);

Upvotes: 2

Hunter McMillen
Hunter McMillen

Reputation: 61515

It might be easier just to assume that the first letter is always lowercase, then you don't need any checks:

String s         = "some string";
String capitol   = Character.toString(s.charAt(0)).toUpperCase();
String newString = capitol + s.substring(1,x);

Upvotes: 6

Related Questions