Jump to content
xisto Community
Sign in to follow this  
xenador

Scanner Input Via Nextline run into an error with scanner input

Recommended Posts

so my understanding of a scanner is that it is an easy way to get input. I am dealing with user input via system.in and the scanner atm some of the input are strings with spaces in them so I cannot reference just key.next() (assuming: Scanner key = new Scanner(System.in):lol: although this had been the way I was used to dealing with user input from before. So I ventured out into some unknown territory, namely key.nextLine() and I assumed that would give me all of the input that the user typed in before hitting enter. Instead it gave a couple things, most importantly my reference to the string that I had threw a StringIndexOutOFBoundsException at index 0 so it appears that what I type into the console isnt getting assigned to the string...any pointers?

Share this post


Link to post
Share on other sites

StringIndexOutOfBoundsException is thrown when you attempt to invoke a method upon a string where the parameters passed would require accessing characters of the string which do not exist, such as negative numbers and those exceeding it's length (and possibly equal to). The JVM should automatically give information about the exception such as the line on which it occurred (I believe), but if it does not, then you can use try/catch to make it easier to pin down the lines which are causing the exception.

Share this post


Link to post
Share on other sites

Nabb: I realize everything you are saying is true, but I already knew that. My problem is not finding out what is throwing the error, rather I want to know why input via a Scanners.nextLine() invocation would ever return without a character at zero. Error handling in place...I guess an empty string could cause the problem but Atm I am the user and I can assert that the entry I tried initially was a valid String. Specifically "Hello world!". So I assume it is due to my lack of understanding of how NextLine() works...I assumed it would operate like a call to .next() except allow for white space(in this case a single space).

Edited by xenador (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.