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?
Change the "{"
to '{'
(your want a character literal, not a string literal).
See Character and String Literals in the tutorial .
Use
System.out.println(code[0].charAt(0) == '{');
instead, ""
is a String
将其更改为单引号以作为字符进行比较。
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) == '{');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.