CharAt()


#1

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


#2
    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

#3

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


#4

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

console.log(isValStart('hello'));
console.log(isValStart('Hello'));

Output

Native Browser JavaScript
 >   
 false
 true
=> undefined   

#5

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

xxx


#6

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.


#7

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

String h myChar = charAt(0);

or

h myChar = charAt(0);


#8

Since h is type cast in the parameter.

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


#9

xxx

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


#10

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.


#11

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


#12

Found this…

Character.isUpperCase(s.charAt(0))

so,

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:

Main.java:1: error: class SomeText is public, should be declared in a file named SomeText.java
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.


#13

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


#14

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