Results 1 to 17 of 17
  1. #1
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Question Count letter occurred in a word ~

    Java Code:
        public static int countLetter(String str, char ch){
        	
        	int count = 0;
        	
        	for(int i=0; i<str.length(); i++){
        		if(str.charAt(i).equals(ch)){
        			count++;
        		}
        	}
        	
        	return count;
        	
        }
    Based on the note that i study (UML Diagram of java.lang.String and java.lang.Character)
    .charAt() function will return specific character in a String but ...
    (str.charAt(i).equals(ch))
    Compiler told me that character cannot be dereferenced.
    I thought that "str.charAt(i)" will return character data type and .equals(ch) will compare it with "ch" variable by using the .equals function in java.lang.Character ... but compiler tell me that cannot be dereferenced, so means that the compiler is using the .equals function from java.lang.String?

  2. #2
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    Hmm ... Seems i have misunderstood between char and Character .... Both of them are different?

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    yes .... char is primitive type while Character is referenced ... make your ch parameter as referenced type ... change line 6 with
    Java Code:
    ch.equals(str.charAt(i))
    I hope it will work

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Count letter occurred in a word ~

    Yup, a char is a primite (like int, long etc); it is just a two byte unsigned int while a Character is a simple class that 'wraps' a char (read: has a char as its single member variable). chars can be compared as the other primitives, i.e. by using the comparison operators (==, != , <, > etc)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    you can also use Character.valueOf() to convert str.charAt(i) to character object first and then compare ...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Count letter occurred in a word ~

    Quote Originally Posted by allaudin View Post
    yes .... char is primitive type while Character is referenced ... make your ch parameter as referenced type ... change line 6 with
    Java Code:
    ch.equals(str.charAt(i))
    I hope it will work
    Have you checked your answer? Hoping is not suficient.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    And by == method as well, as mentioned by josAH :)

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    Have you checked your answer? Hoping is not suficient.

    Jos
    Yup .I'm sure ..

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Count letter occurred in a word ~

    Quote Originally Posted by allaudin View Post
    you can also use Character.valueOf() to convert str.charAt(i) to character object first and then compare ...
    Don't overcomplicate this small problem; I'll leave it to you to explain the autoboxing mechanism to the OP.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Count letter occurred in a word ~

    Quote Originally Posted by allaudin View Post
    Yup .I'm sure ..
    You're lying; your suggestion doesn't even compile and the compiler tells you exactly what is wrong with the code. Have you actually checked your answer before posting you wouldn't have mislead the OP and obfuscated this thread.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    You're lying; your suggestion doesn't even compile and the compiler tells you exactly what is wrong with the code. Have you actually checked your answer before posting you wouldn't have mislead the OP and obfuscated this thread.

    Jos
    Now I checked that ... its working fine ...

  12. #12
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    You're lying; your suggestion doesn't even compile and the compiler tells you exactly what is wrong with the code. Have you actually checked your answer before posting you wouldn't have mislead the OP and obfuscated this thread.

    Jos
    Have you tried it ? or you wana make me to verify my answer ?

  13. #13
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    make your ch parameter as referenced type ...
    I told him to do this first

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Count letter occurred in a word ~

    Quote Originally Posted by allaudin View Post
    I told him to do this first
    Yes, because you wrote that yourself in reply #3 and it is dead wrong; stop posting nonsense.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Count letter occurred in a word ~

    so ?? I was right ...

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Count letter occurred in a word ~

    @JosAH: remember the last time I said "we'll have to agree to disagree"? Well guess what... I take it back, I agree.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Count letter occurred in a word ~

    Quote Originally Posted by allaudin View Post
    so ?? I was right ...
    No you were not; your attempt to a solution is a lousy attempt; the Character class can only cache the characters 0x00 ... 0x7f; all the others are freshly instantiated; and all that crap because you don't know (or don't want to know) how to compare two simple primitives. If I had to review your code, I'd cross it out completely with my red pencil; in the future, think before you start blabbering nonsense in this forum. If the OP ever returns here with a question about massive memory usage or what exactly autoboxing does I'm pointing the OP to you; you make a mess, you clean up the mess.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. search on a word with one letter
    By raphaelverdier in forum Lucene
    Replies: 0
    Last Post: 08-16-2013, 08:42 AM
  2. How to define what a word is for a word count?
    By Fluxtime in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 10:59 AM
  3. Word Count in Java
    By michaelangelokrk in forum New To Java
    Replies: 4
    Last Post: 09-22-2011, 08:34 AM
  4. how do i count the letter that the user had chosen?
    By ashin in forum Advanced Java
    Replies: 4
    Last Post: 10-08-2009, 05:00 PM
  5. how do i initials the word so i get the first letter only
    By mikeitalydz in forum New To Java
    Replies: 15
    Last Post: 04-19-2009, 10:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •