aspose file tools*
The moose likes Beginning Java and the fly likes java.lang.StringIndexOutOfBoundsException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.StringIndexOutOfBoundsException" Watch "java.lang.StringIndexOutOfBoundsException" New topic
Author

java.lang.StringIndexOutOfBoundsException

barry richard
Greenhorn

Joined: Oct 03, 2012
Posts: 19
Hi, beginner here

This is the code that I am trying to run



It is supposed to count all different letters in a string

This is what i get


What am I doing wrong

thanks for your time
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The error message is telling you exactly what you're doing wrong.

It's telling you that at line 25 of Example.java, you're calling String.charAt(), and you're trying to get the 6th character (at index 5) of a String that has fewer than 6 characters.

So you need to either user a longer String, or not try to get the 6th character.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

barry richard wrote:It is supposed to count all different letters in a string
What am I doing wrong

Simply put, you're NOT counting the number of different (by which I presume you mean distinct) letters in the String.

Forget about letters for a moment and consider how you would count the number of distinct values in ANY kind of set.

Dunno about you, but if it was me, I'd look at it as something like:
{number of distinct values} = {number of actual values} - {number of duplicate values}

So now all you have to work out is how to determine a "duplicate".

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Try this:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11418
    
  16

don't you want



less than s.length(), not less than or equal to s.length()


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:don't you want



less than s.length(), not less than or equal to s.length()


No, I want it exactly as it is. It's an SSCCE to show him what his code is doing.
barry richard
Greenhorn

Joined: Oct 03, 2012
Posts: 19
Thanks for your answers guys
Jeff, when I run your code


I still get



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

barry richard wrote:Thanks for your answers guys
Jeff, when I run your code


I still get





Yes, I know. That's what it's supposed to do. It's your code, with print statements added, so that you can see where you're going wrong. Look at it closely. Make sure you understand how the for loop control corresponds to the output we're seeing.

In particular, note that an array of length 3 has indices 0, 1, and 2. If it had index 3, it would have a lenght of 4.
barry richard
Greenhorn

Joined: Oct 03, 2012
Posts: 19
Ok I get it, it is an issue with index numbers being less than the length

like:
length = 5
index 0 to 4
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11418
    
  16

barry richard wrote:
I still get




so put it another way...what do you expect to print for s.charAt(3);, if you have already printed the a, b, and the c?

update - you replied while i was typing mine...

barry richard wrote:Ok I get it, it is an issue with index numbers being less than the length

correct. The standard idiom would be to loop to LESS THAN the length:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:
so put it another way...what do you expect to print for s.charAt(3);, if you have already printed the a, b, and the c?


Well put. I need to learn to be more succinct like that.
barry richard
Greenhorn

Joined: Oct 03, 2012
Posts: 19
Thanks

I am learning java as my first language all by myself (I know!)

So I think I will be making some (actually a lot!) of silly mistakes along the way.

But i will get there. I always have.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.lang.StringIndexOutOfBoundsException