-1

I have been solving some hackerank basic JAVA question , one of the problem statement had following piece of code:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        scanner.close();
        if(N%2!=0){
            System.out.println("Weird");
        }
        else if(N%2==0 && N<=5 && N>=2){
             System.out.println("Not Weird");

        }
        else if(N%2==0 && N<=20 && N>=6){
             System.out.println("Weird");
    }
    else if(N%2==0 && N>20){
             System.out.println("Not Weird");
}}}

Can you explain what does "scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?")" I know it skip certain patterns but what are in the parenthesis ?

Nicolas Jit
  • 66
  • 1
  • 5
  • 1
    Are you talking about the `\u...` notation? These are escape sequences for unicode characters. See https://www.fileformat.info/info/unicode/char/2028/index.htm, https://www.fileformat.info/info/unicode/char/2029/index.htm and https://www.fileformat.info/info/unicode/char/0085/index.htm – Progman Sep 04 '22 at 16:46
  • I came across your post when looking for a quick answer to this same exact question. Thank you for daring to ask this seemingly trivial question. – WebViewer Nov 01 '22 at 15:12

1 Answers1

6

It roughtly means skip newlines

\n, \r, \n\r, \r\n, U+0085 U+2028 U+2029 are all sequences of characters or characters used as line separators.

\n is LF or Line Feed or NewLine
\r is CR or Carriage Return 
\r\n CR+LF is used in Windows as newLine sequence
U+0085 NEL is the Unicode character for NExt Line
U+2028 is the Unicode character for Line Separator
U+2029 is the Unicode character for Paragraph Separator

All of those were used to break a file in lines. What you see there is a Regex pattern, used to search inside strings. It's read as:

If you meat \n\r
or |
one of the characters in the group **[\n \r \u2028 \u2029 \u0085]** (those are considered single characters to choose from)
? 0 or 1 times

Regex is a very powerful tool. If you need to search specific pattern in a string, I suggest you to learn it.

Luca Scarcia
  • 208
  • 1
  • 11
  • thanks a lot mate ! just to be confirmed, is the only purpose of using this scanner.skip() to prevent unwanted string or input from the user? – Nicolas Jit Sep 04 '22 at 17:20
  • @NicolasJit The `\r\n|[\n\r\u2028\u2029\u0085]` is attempting to match line-separators. It is very similar to `\R` added to regex in Java 8 which purpose is precisely to match different line separators (more info: https://stackoverflow.com/a/31060125). Anyway `skip("\\R")` (or in your case `scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?")`) is usually helpful in situation when you want to use `nextLine` but you don't know if it is "safe". I tried to explain it in https://stackoverflow.com/a/39949330. In your case the `skip` is redundant since scanner is closed immediately after. – Pshemo Sep 04 '22 at 18:04
  • 1
    @NicolasJit more than prevent is to filter out. Like when you read an input from System.in.read, you have the string ending with \n (the output of the enter key). Normally you should remove it manually. This function removes it for you. But. more than from System.in, I'd say it's from a file, because `\r\n \u2028 \u2029 and \u0085` are not output from keyboard. You can imagine it as a more powerful version of `BufferedReader::readLine()` – Luca Scarcia Sep 05 '22 at 13:11