• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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!
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that smells like a codingbat question to me
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic