aspose file tools*
The moose likes Beginning Java and the fly likes Need some help about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need some help about ".charAt" and ".equals" just a newbie." Watch "Need some help about ".charAt" and ".equals" just a newbie." New topic
Author

Need some help about ".charAt" and ".equals" just a newbie.

Marvin Porte
Greenhorn

Joined: Nov 11, 2012
Posts: 11
Hello! I just started studying java. And I'm completely new in programming. I'm practicing to code with some problems given by some websites that i searched. Here's the problem.

PROBLEM: Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx" contains 2 "xx".

MY SOLUTION:


ERROR:

Compile problems:

Error: if((str.charAt(i)).equals('x')){
Cannot invoke equals(char) on the primitive type char

I can't understand the error. For me it must run just fine. Please Explain to me the meaning. Thanks!
Sorry if that's too easy for you. I'm really just inexperienced.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

As the API states for the equals method:
Indicates whether some other object is "equal to" this one.


So you use equals only when you want to compare two object references. char is primitive type, and you can't invoke equals method on variable of char type. Thus, you compare it as you would compare two variables of any other primitive type:


Edit: Also, please UseCodeTags when posting your code, it will be easier to read. And welcome to the Ranch!


The quieter you are, the more you are able to hear.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8215
    
  23

Marvin Porte wrote:Error: if((str.charAt(i)).equals('x')){
Cannot invoke equals(char) on the primitive type char
I can't understand the error. For me it must run just fine. Please Explain to me the meaning. Thanks!

There are two major types of variable in Java: primitives and reference types (or 'objects').

A char is a primitive; a Character (or indeed a String) is an object, and therefore a reference type. You use '==' with primitives and .equals() with objects. String.charAt() returns a char, so the message was telling you that .equals() is not appropriate.

So your condition should be:
if (str.charAt(i) == 'x') { ...

However, it's nice to see a beginner err on the side of using equals() rather than the other way around. Keep it up.

You can also string the two conditions together with '&&' rather than nesting them.

Minor efficiency point: You are allowed to define more than one variable in a for loop, viz:
for(int i = 0, e = str.length()-1; i < e; i++) { ...
which saves the loop having to evaluate str.length()-1 every time.

Winston

PS: You might also want to look at String.indexOf(String, fromIndex) because it'll save you having to do the second check; but what you have looks fine, so don't worry about it too much.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8215
    
  23

Kemal Sokolovic wrote:Edit: Also, please UseCodeTags when posting your code, it will be easier to read...

@Marvin: I've added them for you this time. See how much better it looks? You can click on the UseCodeTags link for more information.

Winston
Marvin Porte
Greenhorn

Joined: Nov 11, 2012
Posts: 11
@Kemal and @Winston
Thank you so much! I understand your explanations.

@Winston
Sorry sir, I forgot to put code tags.

I only make codes from what I've learned so far. And I really worry for those static methods in API. My code's become longer because of not knowing those. And I think my coding becomes limited. >.<

Thank you Sir Kemal and Sir Winston!
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
that smells like a codingbat question to me
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need some help about ".charAt" and ".equals" just a newbie.