I'm trying to capitalize the first character of a string. I've seen other articles on stack overflow, and tried the Apache common packages. However, the output stays lower case, and unmodified. Here's my code;
package name;
import java.util.Scanner;
import java.lang.Object;
import org.apache.commons.lang3.text.WordUtils;
public class Name {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("What is your first name?");
String first = input.nextLine();
System.out.println("What is your last name?");
String last = input.nextLine();
String full = (first + " " + last);
WordUtils.capitalize(full);
System.out.println("Your name is " + full);
input.close();
}
}
I've also tried
System.out.println("What is your first name?");
String first = input.nextLine();
WordUtils.capitalize(first);
System.out.println("What is your last name?");
String last = input.nextLine();
WordUtils.capitalize(last);
System.out.println("Your name is " + first + last);
I tried using capitalzieFully, but that yielded no results either. (I'm aware of Object not being used, I just tried importing that as a test).
String is immutable in java.
first= WordUtils.capitalize(first);
So you have re assign it to first that returned by the capitalize method.
String first= "test";
WordUtils.capitalize(first);
//Above method returns a new String,you are not receiving that
// Still first is "test" because String is immutable.
first= WordUtils.capitalize(first);
//Now first = "TEST"
And also do the same in remaining places.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With