File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Scanner class issues 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 » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Scanner class issues" Watch "Scanner class issues" New topic
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-:

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".


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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner class issues
 
Similar Threads
confused about Scanner class
Scanner Example from K&B.
Scanner Example from K&B.
Tokenizing with scanner
Tokenizing with Scanner