There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
So I tried to do something different. I wanted to actually print initials (first letters next to each other). I wanted to save charAt() to temporary variables but i get an error. Why it doesn’t work? I’m confused…
public class Initials {
public static void main(String[] args) {
// Add a first name and a last name:
String firstName = "James";
String lastName = " Franco";
// What are the initials?
fName = firstName.charAt(0);
lName = lastName.charAt(0);
System.out.println(fName.concat(lName));
}
}
You must declare a new string and concat the two strings in that new variable and then return/print it.
public class ConcatenationExample {
public static void main(String args[]) {
//One way of doing concatenation
String str1 = "Welcome";
str1 = str1.concat(" to ");
str1 = str1.concat(" String handling ");
System.out.println(str1);
//Other way of doing concatenation in one line
String str2 = "This";
str2 = str2.concat(" is").concat(" just a").concat(" String");
System.out.println(str2);
}
}
Your first attempt at this challenge was closer to the solution. Your code is correct with the exception of one error.
You’ve forgotten to declare the data types of fName and lName. However, because they are char types, .concat() will not work since this method only works on strings. With this in mind, what do you need to do achieve your goal?
I had the same issue, I looked through the responses and didn’t really find a definitive answer, so I Googled it up and found what I think is a more simple and satisfying answer. As someone else mentioned, when the compiler sees your code, it converts each char to an int and adds them, which is not what you’re looking for. You could initialize two “middleman” string variables, assign them using charAt(0) on firstName and lastName and then add those two together within the print command as some have suggested, or you could also use two System.out.print commands (rather than System.out.println) if you want the initials to appear side-by-side, but I find this to be an even better solution:
When you put the empty quotes at the beginning, it tells the compiler you want to treat the operation as a string addition (I assume because as soon as a string is involved, even if empty, the only way to perform the operation is to treat every item as a string). This way you can do it all in the print command without having to introduce any extra variables. Easy!
System.out.println(String.valueOf(firstName.charAt(0)) + String.valueOf(lastName.charAt(0)));
// OR
System.out.println(String.valueOf(firstName.charAt(0)) + lastName.charAt(0));
This can make your code more readable since it’s a bit clearer that you’re trying to get the string value of the initials rather than the Unicode codes. In the above code I posted, the first option is more clear than the second as well.