• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

error in programe

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(edited to format code)
[This message has been edited by Cindy Glass (edited October 24, 2001).]
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you reffering to the spelling errors
or to the fact you posted this in the worng forum???
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic