This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Getting an error from a charAt command Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Getting an error from a charAt command" Watch "Getting an error from a charAt command" New topic
Author

Getting an error from a charAt command

Alex Ba
Ranch Hand

Joined: Oct 14, 2009
Posts: 35


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
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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

Joined: Oct 14, 2009
Posts: 35
But it prints "Please Vote" and then doesn't give an opportunity to fill q...
Alex Ba
Ranch Hand

Joined: Oct 14, 2009
Posts: 35
??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16692
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting an error from a charAt command
 
Similar Threads
Java-4a Please
Java adding infinte strings
Return Statements problem
NPE from K&B example SCJP6
returning an array of characters