This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Scanner class issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
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: 1464
    
    5

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, OCEEJBD)
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: 1464
    
    5

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: 923
    
    1

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 %) OCPJBCD(93%)
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
Tokenizing with Scanner
Tokenizing with scanner
Scanner Example from K&B.
confused about Scanner class
Scanner Example from K&B.