(edited to format code) [This message has been edited by Cindy Glass (edited October 24, 2001).]
Joined: Jun 01, 2000
are you reffering to the spelling errors or to the fact you posted this in the worng forum???
Joined: May 10, 2001
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
Joined: Jan 07, 1999
I think this thread is more appropriate to the java in general (Beginner) forum, so I've moved it for you.
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
Joined: Sep 14, 2001
Thanks Cindy, I have a question about a data member (or is it called this) called length that returns the number of elements in an array. How come this is not a method since it returns a value?
I probably should post a new topic for this but since you brought the length keyword here, I thought I'll do it here.
Joined: Sep 23, 2001
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).
Joined: Jan 30, 2000
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.