[FIXED] What is scanner skip in Java and why to use it?



Can someone explain, what does the above code mean.

I’m pretty new to Java.

Any help would be greatly appreciated.



public Scanner skip(Pattern pattern)

Skips input that matches the specified pattern, ignoring delimiters. This method will skip input if an anchored match of the specified pattern succeeds.
If a match to the specified pattern is not found at the current position, then no input is skipped and a NoSuchElementException is thrown.

Since this method seeks to match the specified pattern starting at the scanner’s current position, patterns that can match a lot of input (“.*”, for example) may cause the scanner to buffer a large amount of input.

So this allows you to “move” the scanner position using a regex.

Example :

Skip the start of the line :

Scanner scan = new Scanner("Hello world");
scan.skip("Hello ");



Since this is using a regex, you skip until a work in the middle of a line :

Scanner scan = new Scanner("Hello world, I am happy to see you");
scan.skip(".*I am ");


happy to see you

Answered By – AxelH

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published