Convert strings in an array to Title Case - JAVA
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"
java
add a comment |
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"
java
1
Hint: What doesc.getFirst_name().charAt(0)
return?
– John3136
Nov 12 '18 at 22:19
Give an example of what you want to achieve.
– forpas
Nov 12 '18 at 22:19
It looks a duplicate of stackoverflow.com/questions/1086123/…
– AtulK
Nov 12 '18 at 22:20
What happens to McBrides and OConnells?
– Perdi Estaquel
Nov 12 '18 at 22:40
and "von Smith" or "de Smet" or "van de Kasteele"
– YoYo
Nov 12 '18 at 23:30
add a comment |
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"
java
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"
java
java
asked Nov 12 '18 at 22:16
William LovelessWilliam Loveless
155
155
1
Hint: What doesc.getFirst_name().charAt(0)
return?
– John3136
Nov 12 '18 at 22:19
Give an example of what you want to achieve.
– forpas
Nov 12 '18 at 22:19
It looks a duplicate of stackoverflow.com/questions/1086123/…
– AtulK
Nov 12 '18 at 22:20
What happens to McBrides and OConnells?
– Perdi Estaquel
Nov 12 '18 at 22:40
and "von Smith" or "de Smet" or "van de Kasteele"
– YoYo
Nov 12 '18 at 23:30
add a comment |
1
Hint: What doesc.getFirst_name().charAt(0)
return?
– John3136
Nov 12 '18 at 22:19
Give an example of what you want to achieve.
– forpas
Nov 12 '18 at 22:19
It looks a duplicate of stackoverflow.com/questions/1086123/…
– AtulK
Nov 12 '18 at 22:20
What happens to McBrides and OConnells?
– Perdi Estaquel
Nov 12 '18 at 22:40
and "von Smith" or "de Smet" or "van de Kasteele"
– YoYo
Nov 12 '18 at 23:30
1
1
Hint: What does
c.getFirst_name().charAt(0)
return?– John3136
Nov 12 '18 at 22:19
Hint: What does
c.getFirst_name().charAt(0)
return?– John3136
Nov 12 '18 at 22:19
Give an example of what you want to achieve.
– forpas
Nov 12 '18 at 22:19
Give an example of what you want to achieve.
– forpas
Nov 12 '18 at 22:19
It looks a duplicate of stackoverflow.com/questions/1086123/…
– AtulK
Nov 12 '18 at 22:20
It looks a duplicate of stackoverflow.com/questions/1086123/…
– AtulK
Nov 12 '18 at 22:20
What happens to McBrides and OConnells?
– Perdi Estaquel
Nov 12 '18 at 22:40
What happens to McBrides and OConnells?
– Perdi Estaquel
Nov 12 '18 at 22:40
and "von Smith" or "de Smet" or "van de Kasteele"
– YoYo
Nov 12 '18 at 23:30
and "von Smith" or "de Smet" or "van de Kasteele"
– YoYo
Nov 12 '18 at 23:30
add a comment |
3 Answers
3
active
oldest
votes
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()));
}
add a comment |
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.
add a comment |
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.
That is all true, but how is it relevant to the question? He is trying to pass a string to asetFirst_name
method which likely just saysthis.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'stoTitle()
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 achar
not aString
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
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270890%2fconvert-strings-in-an-array-to-title-case-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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()));
}
add a comment |
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()));
}
add a comment |
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()));
}
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()));
}
edited Nov 12 '18 at 22:39
answered Nov 12 '18 at 22:30
forpasforpas
11k2423
11k2423
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 13 '18 at 5:10
answered Nov 12 '18 at 22:58
Perdi EstaquelPerdi Estaquel
6631519
6631519
add a comment |
add a comment |
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.
That is all true, but how is it relevant to the question? He is trying to pass a string to asetFirst_name
method which likely just saysthis.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'stoTitle()
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 achar
not aString
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
|
show 1 more comment
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.
That is all true, but how is it relevant to the question? He is trying to pass a string to asetFirst_name
method which likely just saysthis.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'stoTitle()
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 achar
not aString
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
|
show 1 more comment
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.
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.
answered Nov 12 '18 at 22:26
DonatDonat
699127
699127
That is all true, but how is it relevant to the question? He is trying to pass a string to asetFirst_name
method which likely just saysthis.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'stoTitle()
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 achar
not aString
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
|
show 1 more comment
That is all true, but how is it relevant to the question? He is trying to pass a string to asetFirst_name
method which likely just saysthis.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'stoTitle()
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 achar
not aString
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
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
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
@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
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
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
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
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270890%2fconvert-strings-in-an-array-to-title-case-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Hint: What does
c.getFirst_name().charAt(0)
return?– John3136
Nov 12 '18 at 22:19
Give an example of what you want to achieve.
– forpas
Nov 12 '18 at 22:19
It looks a duplicate of stackoverflow.com/questions/1086123/…
– AtulK
Nov 12 '18 at 22:20
What happens to McBrides and OConnells?
– Perdi Estaquel
Nov 12 '18 at 22:40
and "von Smith" or "de Smet" or "van de Kasteele"
– YoYo
Nov 12 '18 at 23:30