Results 1 to 11 of 11
  1. #1
    Tiff89 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Exclamation Counting characters

    Hello, I just wanted to know what I could be doing wrong. I have this code here called numCharacter() which is supposed to count all the characters from an imported file. However, an error keeps occurring. Please help?



    Java Code:
        
          public int numCharacters() {
         
                while (count != -1) {
                   int charPos = indexOf(count, ch);
                   if (charPos != -1) {
                       count++;
                }
            }
        }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what's the error? what is indexOf()? what is ch? what is count?

  3. #3
    Tiff89 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    I was using the API for String and reading the description I thought indexof() would be appropriate.


    and the error is that it does not compile how I wanted it to.

    honestly, I thought i was going in the correct path. But if there is a better way, please direct me to it.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You can do simply something like this.

    Java Code:
        private int charCount(String str) {
    
            String newStr = str.trim();
            int whiteSpace = 0;
    
            for(int i = 0; i < newStr.length(); i++) {
                if(Character.isWhitespace(newStr.charAt(i))) {
                    whiteSpace++;
                }
            }
    
            return (newStr.length() - whiteSpace);
        }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tiff89 View Post
    I was using the API for String and reading the description I thought indexof() would be appropriate.


    and the error is that it does not compile how I wanted it to.

    honestly, I thought i was going in the correct path. But if there is a better way, please direct me to it.
    Are you sure that you have read the whole API on String class?

    indexOf() has four overloaded methods and each of them are process in different ways with different arguments. Basically it returns the index of the first occurrence index of defined character.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Please...

    Eranga... I know you're trying to help, but at the level of Java/programming the OP is showing (look at the given example) do you really think your example will be understood ? I would suggest you try to explain that example you posted so the OP can understand what is going on.

    BTW, I think that is a very good/faster way of counting characters in a string.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by CJSLMAN View Post
    Eranga... I know you're trying to help, but at the level of Java/programming the OP is showing (look at the given example) do you really think your example will be understood ? I would suggest you try to explain that example you posted so the OP can understand what is going on.

    BTW, I think that is a very good/faster way of counting characters in a string.

    CJSL
    Sure, I can explain it line by line in full details. But my approach is different lol. :)

    I know OP may be can mess with my code, look at my very next replay on this. He says that he read String API and the way he coded is correct, actually he thinks like that. But seems to me it's not.

    If yes, he must know what trim(), indexOf(), length(). Because API explain all of them more clearly. He can stuck with wrapper class usage in my code. I'll wait until he's get back about that here. Because I want him to make think and search more and read more about that lol.

    I think you may seen that my my posts. I've never give codes in very first replay if he/she not work on with APIs doc. I want to see all of them become keen people on Java. :)

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default OK...

    OK... let's see what the OP thinks...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by CJSLMAN View Post
    OK... let's see what the OP thinks...

    CJSL
    I don't think you are angry with my comments. :) I'm not hang on here in this community just to post replays lol. I want to share my knowledge with all others and really like to see all of them are better than me on Java.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default nope

    Heavens no, I'm not angry :D. I really mean it. Let's see what the OP has to say. If he/she comes back and indicates that the provided code is understood, that's great. If he/she doesn't understand it, then it will have to be explained.
    Hey.. no problem. I'm here to help and learn. This is a great forum and I want to keep it that way.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  2. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 07:33 PM
  3. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 01:33 PM
  4. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 05:51 PM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 05:45 PM

Posting Permissions

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