After Java inputs -a,-b,geek,-c,888,-d,[hello,world] characters, how can [hello,world] not be separated?

Insert image description here

After Java inputs the characters -a,-b,geek,-c,888,-d,[hello,world], how can [hello,world] not be separated?

You can use the command line parameter parsing library to handle individual parameters in Java input. In this case, you can use the Apache Commons CLI library to parse command line arguments. Here is an example code snippet demonstrating how to parse the input and keep [hello, world] as a whole:

import org.apache.commons.cli.*;

public class Main {
public static void main(String[] args) {
Options options = new Options();
options.addOption(“a”, false, “Option A”);
options.addOption(“b”, true, “Option B”);
options.addOption(“c”, true, “Option C”);
options.addOption(“d”, true, “Option D”);

    CommandLineParser parser = new DefaultParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("a")) {
            System.out.println("Option A is present.");
        }

        if (cmd.hasOption("b")) {
            String value = cmd.getOptionValue("b");
            System.out.println("Option B value: " + value);
        }

        if (cmd.hasOption("c")) {
            String value = cmd.getOptionValue("c");
            System.out.println("Option C value: " + value);
        }

        if (cmd.hasOption("d")) {
            String value = cmd.getOptionValue("d");
            System.out.println("Option D value: " + value);
        }

        String[] remainingArgs = cmd.getArgs();
        if (remainingArgs.length > 0) {
            String lastArg = remainingArgs[remainingArgs.length - 1];
            System.out.println("Last argument: " + lastArg);
        }
    } catch (ParseException e) {
        System.out.println("Command line parse error: " + e.getMessage());
    }
}

}
With this code snippet, you can parse the input and keep [hello,world] as a whole. In the command line, run the above code using -a -b geek -c 888 -d [hello,world] as parameters, then you will get the following output:

Option A is present.
Option B value: geek
Option C value: 888
Option D value: [hello,world]

After Java inputs -a,-b,geek,-c,888,-d,[hello,world] characters, will it be stored in an array?

You can convert the input parameters into an array of strings and store them sequentially. Here is a sample code that demonstrates how to store input parameters in a Java string array:

public class Main {
    
    
    public static void main(String[] args) {
    
    
        String[] input = {
    
    "-a", "-b", "geek", "-c", "888", "-d", "[hello,world]"};

        // 打印原始输入
        System.out.println("原始输入:");
        for (String arg : input) {
    
    
            System.out.println(arg);
        }

        // 将输入参数存储到数组中
        String[] storedArray = new String[input.length];
        System.arraycopy(input, 0, storedArray, 0, input.length);

        // 打印存储的数组
        System.out.println("存储的数组:");
        for (String arg : storedArray) {
    
    
            System.out.println(arg);
        }
    }
}

With this code snippet you will get the following output:

原始输入:
-a
-b
geek
-c
888
-d
[hello,world]
存储的数组:
-a
-b
geek
-c
888
-d
[hello,world]

In this way, you can store the input parameters in the string array in order for subsequent processing and use.

How does the return value of scanner.next(); become an array?

To store the values ​​returned by the Scanner's next() method into an array, you can use an ArrayList to receive all the values ​​and then convert them to an array. The following is a sample code that demonstrates how to store the Scanner's return value as an array:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> values = new ArrayList<>();

        while (scanner.hasNext()) {
    
    
            String value = scanner.next();
            values.add(value);
        }

        String[] array = values.toArray(new String[0]);

        // 打印数组
        for (String value : array) {
    
    
            System.out.println(value);
        }
    }
}

With this code snippet, you can store each return value of the Scanner in an ArrayList. Then, use the toArray() method to convert the ArrayList into a string array. Finally, you can print the values ​​in the array.

Note that this example will read from standard input until end of file (Ctrl+D) or other termination condition is encountered. You can make appropriate modifications according to your needs.

Enter the string -a,-b,geek,-c,888,-d,[hello,world] in Java. How to keep [hello, world] intact when split into arrays?

When you want to split the input string -a,-b,geek,-c,888,-d,[hello,world] into an array, you can use commas as delimiters and exclude the content within square brackets []. Here's a sample code that demonstrates how to split that string into an array and keep [hello,world] as a whole:

public class Main {
    
    
    public static void main(String[] args) {
    
    
        String input = "-a,-b,geek,-c,888,-d,[hello,world]";

        // 拆分字符串并保留 [hello,world] 作为整体
        String[] array = input.split(",(?![^\\[\\]]*\\])");

        // 打印拆分后的数组
        for (String item : array) {
    
    
            System.out.println(item);
        }
    }
}

With this code snippet you will get the following output:

-a
-b
geek
-c
888
-d
[hello,world]

In the above code, we use the regular expression ",(?![^\[\]]*\])" as the parameter of the split() method. This regular expression uses commas as delimiters and excludes content within square brackets [] when splitting. In this way, we can keep [hello, world] stored in the array as a whole.

Guess you like

Origin blog.csdn.net/weixin_50503886/article/details/132796430