We help IT Professionals succeed at work.

How can I do a string comparison containing charAt?

Eindoofus
Eindoofus asked
on
Medium Priority
301 Views
Last Modified: 2012-05-11
if (postfix.charAt(i).equals("+")) {
}

Open in new window

When I try to compile that line I receive the following compiler error:
Main.java:30: char cannot be dereferenced
            if (postfix.charAt(i).equals("+")) {

Open in new window

"postfix" is a string that I want pull individual characters from and check them against certain conditions such as in this case whether the character at position "i" equals "+". What code would allow me to do that?
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
if (postfix.charAt(i)==equals("+")) {}
Awarded 2011
Awarded 2011

Commented:

sorry, this is correct:
if (postfix.charAt(i)==equals('+')) {}
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011

Commented:
characters are not strings, you don't need equals to comapre them
and in your case you wanted to compare character to string in double quotes - this is a String: "+"

postfix.charAt(i) - this is character
Awarded 2011
Awarded 2011

Commented:
(postfix.charAt(i)=='+') {}
Awarded 2011
Awarded 2011

Commented:
I think this should be split 125/125 between
http:#35464098  and http:#35464103
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
accept 35464098
it clearly answers the question "What code would allow me to do that?"
Awarded 2011
Awarded 2011

Commented:
It would be unfair to accept only one solution.
The main point was to show that == needs to be used,
so the author already after ID:35464092 already understood where the mistake was.
Well, I made formal spelling mistake - this is true, that's why it would be fair to split the points.
Awarded 2011
Awarded 2011

Commented:
Well, that is unfair, what can i say....
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.