Results 1 to 11 of 11
Thread: Counting characters
- 12-12-2008, 08:09 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
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++; } } }
- 12-12-2008, 08:25 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
what's the error? what is indexOf()? what is ch? what is count?
- 12-12-2008, 08:33 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
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.
- 12-12-2008, 08:45 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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); }
- 12-12-2008, 08:48 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-12-2008, 08:54 AM #6
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-12-2008, 09:00 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
- 12-12-2008, 09:03 AM #8
OK...
OK... let's see what the OP thinks...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-12-2008, 09:07 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-12-2008, 09:17 AM #10
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-12-2008, 09:21 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's better lol. We are really valued people like you here in our community lol. :) Keep it up.
Similar Threads
-
Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05-06-2011, 03:03 PM -
Counting Duplicate Variables in an Array
By Npcomplete in forum New To JavaReplies: 2Last Post: 10-24-2008, 07:33 PM -
Need help with counting letters
By mrdestroy in forum New To JavaReplies: 15Last Post: 10-22-2008, 01:33 PM -
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11-29-2007, 05:51 PM -
Counting Vowels and Constonants
By MattN in forum New To JavaReplies: 3Last Post: 11-20-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks