What is the result?

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?

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



Leave a Reply 11

Your email address will not be published. Required fields are marked *


Boris

Boris

wrong answer

Boris

Boris

the correct answer is B

Robertinho

Robertinho

the correct answer is B

Ray

Ray

The correct answer is B

Ray

Ray

\\s* matches 0 or more occurrences of whitespaces.

Tim

Tim

+1

Tim

Tim

However, the useDelimiter(…) should be modified to s.useDelimiter(…).

azzmi

azzmi

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…

wolfatthegate

wolfatthegate

replace the line s.useDelimiter(“,\\s*”);

I compiled the code. I got
Pastel
*Enamel
Fresco
*Gouache

Answer: C