0

so i'm currently trying to check whether or not there's a brace in a String in an array, but I can't figure out how its done.

    public void braceChecker()
    {
         String[] code = new String[]{"{} [] () {"};         
         System.out.println(code[0].charAt(0) == "{");
    }

Obviously, it errors on the last print statement, but I can't find a way to check that character without java yelling at me. If I take "{" out of the string, it interprets it as an actual brace. How do I go about this?

user2789945
  • 527
  • 2
  • 6
  • 23

4 Answers4

3

Change the "{" to '{' (your want a character literal, not a string literal).

See Character and String Literals in the tutorial.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Use

System.out.println(code[0].charAt(0) == '{');

instead, "" is a String

betteroutthanin
  • 7,148
  • 8
  • 29
  • 48
0

Change it to single quotes to compare as a character.

shree.pat18
  • 21,449
  • 3
  • 43
  • 63
0

A char is compared to by using single quotes ' not double quotes " which are used for Strings.

public void braceChecker()
{
     String[] code = new String[]{"{} [] () {"};
     System.out.println(code[0].trim());
     System.out.println(code[0].charAt(0) == '{');
}
baeda
  • 189
  • 12