[daily question] day13_02 parameter analysis

Posted by grandman on Sat, 19 Feb 2022 21:42:05 +0100

Learning objectives:

Objective: to skillfully use the knowledge of Java

Learning content:

Content of this article: using java to solve parameter parsing

Title Description

Enter the following command on the command line:

xcopy /s c:\ d:\,

The parameters are as follows:

Parameter 1: command word xcopy

Parameter 2: string / s

Parameter 3: String c:\

Parameter 4: string d:\

Please write a parameter resolver to resolve the parameters of the command line.

Parsing rules:

1. The parameter separator is a space
2. If there is a space in the middle of the parameter contained by '', it cannot be resolved into multiple parameters. For example, when you enter xcopy /s "C:\program files" "d:" on the command line, the parameters are still 4, and the third parameter should be the string C:\program
Remove the quotation marks when the parameter "C:\program" does not exist, and note that it is not necessary to output it.
3. Parameter variable length
4. The input is guaranteed by the use case, and there will be no input that does not meet the requirements

Problem solving ideas

It is suggested to watch method 2. Method 1 is the answer when I do it myself, which is more complex. Method 2 is that I see the result made by a big man, which is clear

  1. Method 1:
    Use the double pointer to traverse the string. When encountering the double quotation mark, continue to traverse until the next double quotation mark. Insert the string inside the double quotation mark into the queue as a whole. Outside the quotation mark, insert the string between the two spaces into the queue with a space as the separator. Finally, print the elements in the queue
  2. Method 2:
    The single pointer traverses the string and uses a flag flag=1. When double quotation marks are encountered, the operation flag^=1 means that flag=0 inside the double quotation marks and flag=1 outside the double quotation marks. Therefore, when flag=0, all characters are printed. When flag=1, empty spaces are printed as line breaks

Implementation code

  1. Method 1:
public class Parse {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        split(str);
    }
    public static void split(String str) {
        int start = 0;
        int end = 0;
        int length = str.length();
        Queue<String> queue = new LinkedList<>();//Save parsed string
        while (end < length) {
            //Judge whether the string starts with double quotation marks
            if (str.charAt(end) == '"') {
                end = end + 1;//Start with the next character in double quotation marks
                start = end;
                while (end < length && str.charAt(end) != '"') {
                    //Traverse end to the next string
                    end++;
                }
                queue.offer(str.substring(start, end));//Inserts a string in double quotation marks into the queue
                end++;//end points to the next position of the double quotation mark
                continue;
            }
            //Judge the double quotation mark in the middle of the string, because end always points to the space position after each insertion
            //Therefore, you need to judge whether the end+1 position is a double quotation mark
            if (str.charAt(end + 1) == '"') {
                end = end + 2;//Start with the next character in double quotation marks
                start = end;
                while (end < length && str.charAt(end) != '"') {
                    //Traverse end to the next string
                    end++;
                }
                queue.offer(str.substring(start, end));//Inserts a string in double quotation marks into the queue
                end++;//end points to the next position of the double quotation mark
                continue;
            }
            //Point end to the string after the space, that is, the beginning of each parsed string
            while (end < length && str.charAt(end) == ' ') {
                end++;
            }
            start = end;
            //Point end to the end of the parsed string, that is, a space
            while (end < length && str.charAt(end) != ' ') {
                end++;
            }
            queue.offer(str.substring(start, end));//Inserts a string between two spaces into the queue
        }
        //The following is what needs to be printed
        int size = queue.size();
        System.out.println(size);
        while (!queue.isEmpty()) {
            String s = queue.peek();
            queue.poll();
            System.out.println(s);
        }
    }

}
  1. Method 2:
public class Parse {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        split(str);
    }
   public static void split(String str) {
       int count = 0;
       //Get the number of parameters
       for (int i = 0; i < str.length(); i++) {
           //If you encounter double quotation marks, you need to skip the contents
           if (str.charAt(i) == '"') {
               i++;
               while (str.charAt(i) != '"') {
                   i++;
               }
           }
           if (str.charAt(i) == ' ') {
               count++;
           }
       }
       System.out.println(count + 1);
       int flag = 1;//flag=1 means outside the double quotation mark, and flag=0 means inside the double quotation mark
       for (int i = 0; i < str.length(); i++) {
           if (str.charAt(i) == '"') {
               flag ^= 1;
           }
           //1. If a space is found outside the double quotation marks, a new line will be formed
           if (str.charAt(i) == ' ' && flag == 1) {
               System.out.println();
           }
           //2. If you encounter a space in double quotation marks, print the space
           if (str.charAt(i) == ' ' && flag == 0) {
               System.out.print(str.charAt(i));
           }
           //3. Double quotation marks and spaces cannot be printed, because all cases of spaces have been included in the first two cases
           //  The rest can be printed normally
           if (str.charAt(i) != '"' && str.charAt(i) != ' ') {
               System.out.print(str.charAt(i));
           }
       } 
   }
}

Topics: Java queue string