Imran A couple of things: When you have a question or problem with code you should be more specific about exactly what the problem is, we have a lot of visitors here who like to help out but, as far as I know, none of them are mind readers. Also, we have quite a few forums here at the ranch, if you post your queries to the forum that best matches your query then you'll probably get better responses. On the main forum page each forum has a brief description under the forum title. This forum, for example, is for questions and comments about the java ranch site itself. Thirdly, the thing that jumps out first in your code is your declaration main is incorrect. The argument to main should be (String args), if the error you're geting is something along the lines of 'class not found' then that is most likely your cause. hope that helps
------------------ Dave Sun Certified Programmer for the Java� 2 Platform
Actually it does not matter what variable you use for the args array in the main method, so s is just fine. When you tried to get the length of the String str you used s.length which assumes that there is a field in the String object to hold the length. While this IS true for arrays, in all other objects you need to use the method length() to get the result. After I changed that it compiled. However the results of running it were count = count = 2 count = 3 count = 4 count = 5 count = 6 count = 7 count = 8 count = 9 count = 10 count = 11 count = 12 and then a StringIndexOutOfBoundsException You need to move your i++ to after the code where you use it. You initialized it to zero but then incremented it before you started using it as an index for the string. Indexes start at zero so you skipped the first letter the way that you have it. In addition, when you are on the LAST letter of the string, you have code looking at (i + 1), which is of course past the end of the string, so it is "out of bounds" and therefore you got your exception.
"JavaRanch, where the deer and the Certified play" - David O'Meara
if you look in the API, you'll find that length is a member variable, not a method. Therefore it's called like a variable, not with the method parentheses () like in C++. ------------------ Michael J Bruesch Codito, ergo sum... I code, therefore I am. [url = http://www.geocities.com/mjbruesch/games]My Java Games, I'm quite proud[/url]
Michael J Bruesch<br /><i>I code, therefore I am.</i>
Joined: Sep 29, 2000
Java has a few "optimizations" that were done for efficiency. 1. There are some primitives - so Java is not COMPLETELY OO. 2. String has been optimized to reuse the same object multiple times - therefore it had to be defined as immutable 3. There is an array construct that is not your traditional object as an Array would be. Here is a Sun review of arrays. Because an array is not a traditional object - it has a field instead of a method to get the length (as part of the optimization stuff).
Note that the length of an array is a constant in Java, so it's not a problem to use a field rather than a method for this. It's more an issue of consistency - arrays use length, Strings use length(), and Collections use size(), for basically the same idea - so it would have been nice if they'd standardized the usage before releasing the API. But it's too late for that now.