0

I have an array of objects (customer) that has components: first_Name & last_Name. I am trying to convert both the first name and last name of each customer to title case. I have tried making several different methods that will convert strings to title case, but all of them have failed. This is the last thing I have tried and I cannot figure out why it would not capitalize the first letter of the first name and the first letter of the last name.

for (Customer c : customers){

c.setFirst_name(c.getFirst_name().charAt(0).toUpperCase());

}

I have checked that the first name and last name indeed contain an only-letter string with a lower case letter as the first letter in each. The error intellisense is giving me is that "char cannot be dereferenced"

3 Answers3

0

This method capitalizes the 1st char of the string and makes the rest lower case:

public static String toTitle(String s) {
    return (s.length() > 0) ? s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase() : "";
}

so use it:

for (Customer c : customers){
    c.setFirst_name(toTitle(c.getFirst_name()));
    c.setLast_name(toTitle(c.getLast_name()));
}
forpas
  • 160,666
  • 10
  • 38
  • 76
0

String.charAt() returns a char, which is a primitive type and therefore does not have methods. So the toUpperCase() call is not allowed there.

What you probably want is to create a Character object there, maybe a String. We don't know because you never showed the setFirst_name() signature.

Perdi Estaquel
  • 819
  • 1
  • 6
  • 21
-1

String values is immutable. You try to change the first cahracter. That does not work. You must make a new string: Extract the first character of the original string, convert it to upper case and append the rest of the original string.

Donat
  • 4,157
  • 3
  • 11
  • 26
  • 1
    That is all true, but how is it relevant to the question? He is trying to pass a string to a `setFirst_name` method which likely just says `this.firstName = namePassedIn;` – John3136 Nov 12 '18 at 22:30
  • @John3136 Please look at the solution of forpas. T – Donat Nov 12 '18 at 22:45
  • Yes? Your point? forpas's `toTitle()` is not modifying an immutable string it is creating several new strings and combining them to a new one. – John3136 Nov 12 '18 at 22:55
  • forpas is implementing in Java code what I have described in english words. – Donat Nov 12 '18 at 22:58
  • String immutability has nothing to do with the question and is not related to the original issue (`charAt()` returns a `char` not a `String` so you can't call methods on it). As I said - everything your answer says is true, it just doesn't relate to the question. – John3136 Nov 12 '18 at 23:01
  • @John3136 Technically you are completely right. But I tried to guess, what the intention of the statement was. It is a common mistake of beginners in java that they try to modify a string. In other languages it is even possible to assign a value to a substring. Therfore I emphasized the immutablity of String. – Donat Nov 13 '18 at 21:48