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 Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Scanner Class Doubt" Watch "Scanner Class Doubt" New topic
Author

Scanner Class Doubt

Sanjeev Narula
Greenhorn

Joined: Mar 16, 2007
Posts: 19
1. import java.util.*;
2. class Brain {
3. public static void main(String[] args) {
4. // insert code block here
5. }
6. }

Which, inserted independently at line 4, compile and produce the output
"123 82"?
(Choose all that apply.)
A. Scanner sc = new Scanner("123 A 3b c,45, x5x,76 82 L");
while(sc.hasNextInt()) System.out.print(sc.nextInt() + " ");
B. Scanner sc = new Scanner("123 A 3b c,45, x5x,76 82 L").
useDelimiter(" ");
while(sc.hasNextInt()) System.out.print(sc.nextInt() + " ");
C. Scanner sc = new Scanner("123 A 3b c,45, x5x,76 82 L");
while(sc.hasNext()) {
if(sc.hasNextInt()) System.out.print(sc.nextInt() + " ");
else sc.next(); }
D. Scanner sc = new Scanner("123 A 3b c,45, x5x,76 82 L").
useDelimiter(" ");
while(sc.hasNext()) {
if(sc.hasNextInt()) System.out.print(sc.nextInt() + " ");
else sc.next(); }
E. Scanner sc = new Scanner("123 A 3b c,45, x5x,76 82 L");
do {
if(sc.hasNextInt()) System.out.print(sc.nextInt() + " ");
} while ( sc.hasNext() );
F. Scanner sc = new Scanner("123 A 3b c,45, x5x,76 82 L").
useDelimiter(" ");
do {
if(sc.hasNextInt()) System.out.print(sc.nextInt() + " ");
} while ( sc.hasNext() );


Answer is A and C

Can any one explain how ?
Thanks in advance.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,
please find the reply here,

http://www.coderanch.com/t/262215/java-programmer-SCJP/certification/Scanner


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
A is incorrect

A will produce 123 only.


Sanjeev Narula
Greenhorn

Joined: Mar 16, 2007
Posts: 19
hi

Regarding C option why did not it also prints other integers like 3 45 5 76
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Sanjeev,

I have clearly described in my first reply ...

By default Scanner splits given string using " ".
Which ever tokens are integers they will only be displyed.

you can't expect ex: x42y to be displayed as 42.

All the best
Sanjeev Narula
Greenhorn

Joined: Mar 16, 2007
Posts: 19
Thanks Srinivasan .Now i am having better understanding of Scanner Class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner Class Doubt