aspose file tools*
The moose likes Beginning Java and the fly likes How do I compare the contents of a char? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I compare the contents of a char?" Watch "How do I compare the contents of a char?" New topic
Author

How do I compare the contents of a char?

Kristian Andersson
Greenhorn

Joined: Apr 23, 2004
Posts: 7
I got a rather silly assignment in which I am supposed to change all the "com"'s in a String, to "borom"'s. That is, the word "complicated" is supposed to be printed out as "boromplicated". And so on.
Anyway, I'm not asking of how to do it. The problem is, how do I check and compare the contents of a StringBuffer? I am supposed (and I happen to believe that it is also the best way) to use a StringBuffer, created from the original String that I have.
The way I'm thinking of doing this is:
1. Start a loop with the same length as the StringBuffer.
2. Every time in this loop, I want to compare the contents of one place in the StringBuffer to a "c". If it is a "c" then I shall imediately begin to check if the following letters happen to be "o" and "m".
3. If they are, I replace the three letters with "borom".
The code that I have is far from finnished, since the problem occurs quite "early". Anyway, here's the little code that I have managed to create so far:

Oh, almost forgot. Well, maybe not but. In short; the problem can be stated as (forgive me for all of the quite unnessecary chit-chat bove):
"How do I compare the contents of a char?"
....Well. This is the same as the name of the subject. o_O'
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Variables of the primitive type char can be compared using the equality operator ==. They cannot be passed to equals(), which is defined in the class java.lang.Object as taking an Object argument.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Kristian Andersson
Greenhorn

Joined: Apr 23, 2004
Posts: 7
Can you try to explain what passing an Object means? I'm quite lost when it comes to all theese terms. Which is why I don't dare to check the API. I did however, on this assignment.
I've tried to type something like this:
if (charAt(i) == "c")
but it doesn't work. I suppose I am supposed to get hold of the corresponding "char-number" of the letter "c". That is, say the letter "c" is the number "54" in ASCII-code, then I am supposed to check if:
charAt(i) == 54
right? That's quite clever, I should've thought of it before.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Kristian Andersson:

if (charAt(i) == "c")

Two things. First, charAt() is a method of class String, so you need to invoke it on a String, like this: s.charAt(i). Second, "c" is a String that contains one character. A character literal has only single quotes, like 'c'.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I compare the contents of a char?