How could I incorporate CharAt() into code which will return true if a string starts with a capital or false if it doesn’t?

    return str.charAt(0) === str.charAt(0).toUpperCase();

Is one way to do it. This will return true if the character is uppercase, otherwise false.

str => your string


Thanks mtf, I’m struggling with charAt() at the moment as up until now I haven’t seen anything which I thought could help. I’m looking to assign it to this

public boolean isValStart(String h)
      return  h.charAt(0) ==  h.charAt(0).toUpperCase();

Do I need any if statements for your solution? Apologies if some of these questions are silly


No you do not. The expression is boolean since there is a comparison. The evaluation is what will return, either true, or false.

Methods like this are sometimes referred to as pure functions since they give predictible results for all inputs. In this case we assume the input is a string, and Java will like raise an exception if it is not.

Here is a JavaScript representation of the same method (a function in JS)…

const isValStart = str => str.charAt(0) === str.charAt(0).toUpperCase();



Native Browser JavaScript
=> undefined   


This has cleared it up for me, thank you! I’m still having trouble running this however. Here is a screenshot below



I kind of expected that might happen. In all honesty, Java is not my strong suit, and like yourself I struggle with even the little things and have no firm grounding. Its similarity to JavaScript is what gives me the hubris to advise on syntax issues, etc.

Since Java won’t let us dereference the way JavaScript does, then it means caching the character before the boolean expression.

   String myChar = charAt(0);
   return charAt(0) == myChar.toUpperCase();

If that still won’t work, then,

   String myChar = charAt(0);
   myChar = myChar.toUpperCase()
   return charAt(0) == myChar;

And this may even work (so is worth a try)…

   String myChar = charAt(0).toUpperCase();
   return charAt(0) == myChar;

Let me know what errors surface in any event.


Okay I’ll give these a go! When you say string, do I need to input it as

String h myChar = charAt(0);


h myChar = charAt(0);


Since h is type cast in the parameter.

I used String because the variable was being defined. Not sure I got it right, though.



This is the issue which I had with all the solutions. I appreciate your effort though


I messed up, in my haste.

   String myChar = h.charAt(0).toUpperCase();
   return h.charAt(0) == myChar;

Can you show us the class structure to which your method belongs, please. Thanks.


As much as I want to I can’t as the work is part of an assignment, sorry. For some reason they’ve asked me to do this without even covering CharAt()! As for the following code it still doesn’t like the .toUpperCase();


Found this…



public boolean isValStart(String h) {
    return Character.isUpperCase(h.charAt(0));

I’m trying to get it to run on repl it, but get this one error: error: class SomeText is public, should be declared in a file named
public class SomeText {
1 error

exit status 1
public class SomeText {
  public boolean isValStart(String h) {
    return Character.isUpperCase(h.charAt(0));

I’ve never been able to get Java to run there. Has to be a simple reason.


Thanks mate! If I knew you in person I’d buy you a keg of beer! You’re a star!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.