Forums Register Login

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
@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!
+Pie Number of slices to send: Send
that smells like a codingbat question to me
Police line, do not cross. Well, this tiny ad can go through:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 22554 times.
Similar Threads
Java Problem = List of errors, Clueless. HELP
Strings
tic tac toe and switch statement
PrintStream problems
Equals Method problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:35:00.