Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner class issues

 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Morning everyone

Here's some code straight from K&B -:



It compiles fine, but on trying to execute it-:

E:\Java\jdk1.7.0\bin>java ScanNext "1 true 34 hi"
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at ScanNext.main(ScanNext.java:11)


Why is this exception being thrown? The same code in the book is said to have an output "ssssibis2".
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Pritish Chakraborty,

The problem is, because you are providing only one command line argument ("1 true 34 hi" is considered a single argument due to double quotes) and in your code, you are trying to access args[1], and you are getting ArrayIndexOutOfBoundsException.

Are you sure that there are double quotes while running the code, and that there was only one argument?
 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar, I just realized that I messed up.

In the book, both Scanner objects use args[0] as parameter!

I must've misread it while making notes.

Sorry 'bout that.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing to be sorry about.

On the other hand, after hitting 'submit' button, I realized that I've given you almost a ready-made answer which I shouldn't have done
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it like double quotes ESCAPES the space character , due to which javac behaves as if it is just a single argument ? is this related to javac or command line feature of windows , just like in bash we use ""(double quotes), ' '(single quotes) or ``(backquotes) for escaping and executing commands. ?
 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know/remember whether they act as escapes, but yes, they encapsulate one entire String including whitespaces.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic