This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error in programe

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator

(edited to format code)
[This message has been edited by Cindy Glass (edited October 24, 2001).]
 
Fred Abbot
Ranch Hand
Posts: 300
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you reffering to the spelling errors
or to the fact you posted this in the worng forum???
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this thread is more appropriate to the java in general (Beginner) forum, so I've moved it for you.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Margaret Tan
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Bruesch
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic