-2

The actual answer is supposed to be stack and overflow in two separate lines for input stack.overflow; delimiter used is ".". Nothing is shown in the output:

Scanner p = new Scanner(System.in);

p.useDelimiter(".");

System.out.println("delimiter is "+ p.delimiter());

\\this above line is producing expected output

while(p.hasNext()){
    System.out.println(p.next());
}

For input stack.overflow and delimiter "." expected output is

stack
overflow
Armali
  • 18,255
  • 14
  • 57
  • 171

1 Answers1

4
p.useDelimiter(".");

Delimiter is a regex, and . in regex means "every possible character". You are using "every possible character" as delimiter, making Scanner return everything between each pair of characters, which will result in a lot of empty strings.

Escape the dot instead:

p.useDelimiter("\\.");

Output:

delimiter is \.
stack
overflow
BackSlash
  • 21,927
  • 22
  • 96
  • 136