Friday, August 27, 2010

Scanner: ScanStuff.java

Courtesy of Sun Web Learning Center

Given:

1. import java.util.*;
2. class ScanStuff {
3.    public static void main(String [] args) {
4.       String s = "x,yy,123";
5.       Scanner sc = new Scanner(s);
6.       while (sc.hasNext())
7.       System.out.print(sc.next() + " ");
8.    }
9. }

What is the result?


A x yy
B x,yy
C x yy 123
D x,yy,123
E Compilation fails.
F An exception is thrown at runtime.

D is correct. Option C is incorrect because the default separator is a blank NOT a comma.

No comments:

Post a Comment