• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner in java  RSS feed

 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I'm trying to write a basic java program so that when I run it, I can store the names and codes of some of my school textbooks...
I try the scanner statement to input the name of the book but an exception is thrown when I type the name of the book ...
I have attached the code for my program.


 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the ArrayIndexOutOfBoundsException when I try to provide the IMEI number. That's because you start to store them in the array at index of 1 (look at the first for loop and the initial value for variable x). Java arrays start indexing from 0.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, I came up with the same thing.

Regards,
Robert
 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Linhart wrote:I'm getting the ArrayIndexOutOfBoundsException when I try to provide the IMEI number. That's because you start to store them in the array at index of 1 (look at the first for loop and the initial value for variable x). Java arrays start indexing from 0.

Oh OK. I tried writing it to the array from 1st position because I get "Input IMEI 0!!!"
But before that it was from zero and I still get the exception!!!

Thanks for considering!
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you please post the whole actual code together with the output (including exception) you are getting?
 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those places I find Scanner awkward to use. First you need to absorb any left over input (i.e. the new-line after the int). Then you need to read the whole line up to the next new-line.

 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Linhart wrote:Would you please post the whole actual code together with the output (including exception) you are getting?


Here is the new code:



And the exception(I don't get an exception when I write the name of the book without spaces but when I write it with spaces, it gives me an exception):

Non exception run:


Error(exception) run:
 
Vihanga Raj
Greenhorn
Posts: 16
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:This is one of those places I find Scanner awkward to use. First you need to absorb any left over input (i.e. the new-line after the int). Then you need to read the whole line up to the next new-line.




That code worked!
Thank you once again!!!
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop writing all those similes, which are simply annoying, and I have removed them.

The reason for the Exception was that you wrote the for loop wrongly. you should write
for (int i = 0; i < myArray.length; i++)...
because that is guaranteed to iterate the whole of the array and will never raise exceptions. If you need something different you start from that format and change it.

You have some bad design there, using parallel arrays. That is a recipe for disaster. You should use a Product object.
And as CB says, the nextLine method is poorly documented. What does your book says nextLine does?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!