This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting an error from a charAt command  RSS feed

 
Alex Ba
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm getting a Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

at


And I'm not entirely sure why. Place is initialized and set to 0.
 
Paul Clapham
Sheriff
Posts: 22482
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message says there isn't a character at position 0 (which would be the first character) of the string. Based on that you can draw a conclusion about the number of characters in the string.
 
Alex Ba
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it prints "Please Vote" and then doesn't give an opportunity to fill q...
 
Alex Ba
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
??
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Ba wrote:But it prints "Please Vote" and then doesn't give an opportunity to fill q...


If I have to take a guess -- since, you didn't show enough code... I am guessing that kb is a reference to a scanner object. And in other places in the code, you used nextInt(), nextFloat(), or simply next(), which leaves the pointer just after the token. So, when you used readLine(), it read from the last token to the end of the line -- which is simply a blank string, since there is nothing between the end of the last token and the carriage return.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!