| Author |
Scanner class issues
|
Pritish Chakraborty
Ranch Hand
Joined: Jun 12, 2012
Posts: 91
|
|
'Morning everyone
Here's some code straight from K&B -:
It compiles fine, but on trying to execute it-:
Why is this exception being thrown? The same code in the book is said to have an output "ssssibis2".
|
OCJP 6
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
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?
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
Pritish Chakraborty
Ranch Hand
Joined: Jun 12, 2012
Posts: 91
|
|
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
Joined: Dec 08, 2010
Posts: 1295
|
|
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
Joined: Apr 04, 2012
Posts: 869
|
|
|
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. ?
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
Pritish Chakraborty
Ranch Hand
Joined: Jun 12, 2012
Posts: 91
|
|
|
I don't know/remember whether they act as escapes, but yes, they encapsulate one entire String including whitespaces.
|
 |
 |
|
|
subject: Scanner class issues
|
|
|