Given:
import java.util.Scanner;
public class Painting {
public static void main(String[] args) {
String input = “Pastel, *Enamel, Fresco, *Gouache”;
Scanner s = new Scanner(input);
useDelimiter(“,\\s*”);
while (s.hasNext()) {
System.out.println(s.next());
}
}
}
What is the result?
A.
Paste1
Ename1
Fresco
Gouache
B.
Paste1
*Ename1
Fresco
*Gouache
C.
Pastel
Ename1
Fresco
Gouache
D.
Pastel
Ename1, Fresco
Gouache
wrong answer
the correct answer is B
the correct answer is B
The correct answer is B
\\s* matches 0 or more occurrences of whitespaces.
+1
However, the useDelimiter(…) should be modified to s.useDelimiter(…).
B
b
B
it will searches for “,” followed by (zero or more times) white space.
at first we have two parts:
“Pastel, ”
and
“*Enamel, Fresco, *Couache”
so he return the first part after removing the pattern (, ) from it
and repeat the same method again on the part two…
replace the line s.useDelimiter(“,\\s*”);
I compiled the code. I got
Pastel
*Enamel
Fresco
*Gouache
Answer: C