Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.StringIndexOutOfBoundsException

 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't you want



less than s.length(), not less than or equal to s.length()
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers guys
Jeff, when I run your code


I still get



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic