• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

How can I do a string comparison containing charAt?

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?
0
Eindoofus
Asked:
Eindoofus
  • 7
  • 2
1 Solution
 
for_yanCommented:
if (postfix.charAt(i)==equals("+")) {}
0
 
for_yanCommented:

sorry, this is correct:
if (postfix.charAt(i)==equals('+')) {}
0
 
objectsCommented:
if (postfix.charAt(i)=='+') {
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
for_yanCommented:
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
0
 
for_yanCommented:
(postfix.charAt(i)=='+') {}
0
 
for_yanCommented:
I think this should be split 125/125 between
http:#35464098  and http:#35464103
0
 
objectsCommented:
accept 35464098
it clearly answers the question "What code would allow me to do that?"
0
 
for_yanCommented:
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.
0
 
for_yanCommented:
Well, that is unfair, what can i say....
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now