FAQ: String Methods - charAt()

This community-built FAQ covers the “charAt()” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise charAt()

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I expect the following line to result with the initials, “AM”, in the output, but instead outputs “142”? I would like to understand why this is:

    System.out.println(firstName.charAt(0) + lastName.charAt(0)); 
8 Likes
// What are the initials?
    System.out.println(firstName.charAt(0));
    System.out.println(lastName.charAt(0));

my solution… however it prints our the initials on different lines in the output.
very straightforward, the “hint” will give you the answer lol

@betomercado115268528,

char + char = int
Or:
character (Unicode) value + character (Unicode) value = int

Which explains:
charAt() + charAt() = int

This post answers all of my questions:

1 Like

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));
  }
  
}
1 Like

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);
   }
}

Welcome to String handling
This is just a String

Ok so I’ve tried this:

public class Initials {
  
	public static void main(String[] args) {
    
    String firstName = "James";  
    String lastName = " Franco";
    
    String firstName = firstName.concat(firstName.charAt(0));
    String lastName = lastName.concat(lastName.charAt(0));

    System.out.println(firstName.concat(lastName));
  }
  
}

Still doesn’t work

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 did it :smiley:

public class Initials {
  
	public static void main(String[] args) {
    
    
    String firstName = "James";  
    String lastName = "Franco";
    
   
    char fName = firstName.charAt(0);
    char lName = lastName.charAt(0);

    String first = String.valueOf(fName);
    String last = String.valueOf(lName);
    
    System.out.println(first+last);

  }
  
}
  
3 Likes

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:

System.out.println("" + firstName.charAt(0) + lastName.charAt(0));

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!

Welcome to the forums!

Alternatively, you could do the following.

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.

1 Like

I tried

System.out.println(firstName.charAt(0) + lastName.charAt(0));

and it printed… 173. Could you tell me why?