简体   繁体   中英

Checking if there are enough quotation marks in the command line - java

I'm trying to merge to lists of numbers together into one by using command line (args[0] and args[1])

Incidentally, I want to check if there are enough quotation marks (since the required format is ("list of numbers one" "list of numbers two")) to know if I continue on with the rest of the program.

I tried putting args[0] and args[1] in two different strings and checking if they have quotation marks, but they don't.

I was wondering how I can check for the number of quotation marks in the command line.

Here is what I have so far :

String exampleString = "\"12 1 14 -3 7\"" + "\"45 2 16 -6\"";

        int quoteCounter = 0;

        for(int i=0; i < args[0].length(); i++) {
            if(args[0].charAt(i) == '"') {
                quoteCounter++;
                }
            }
        for(int i=0; i < args[1].length(); i++) {
            if(args[1].charAt(i) == '"') {
                quoteCounter++;
                }
            }

        if (quoteCounter != 4) {
            System.out.println("Run the program again and respect the following format please (as many numbers as you want can be inputted) : \"" + exampleString + "\"");
            System.exit(1);
        } else {
            }

You don't need to check for any quotation marks.

If you invoke C:/> run.exe "1 2 3 4 5" "6 7 8 9 10" (ignore the fact that Java generally doesn't generate .exe files as it doesn't matter), the argument list will contain 1 2 3 4 5 and 6 7 8 9 10 (and run.exe , of course), because quotation marked strings ignore spaces.

The args themselves don't get the quotation marks.

Each item in it is one quotation marked string, so you can simply compare the length of the args array to 2.

This code should do the trick:

import java.util.*;

class Main
{
    /* This method checks whether a given argument is valid
     */
    private static boolean isValidArgument(String arg)
    {
        return arg.startsWith("\"") && arg.endsWith("\"");
    }

    /* This method converts an argument into a Set<Integer>
     */
    private static Set<Integer> toIntegerSet(String arg)
    {
        Set<Integer> s = new HashSet<>();
        for(String part : arg.substring(1, arg.length()-1).split(" "))
        {
            s.add(Integer.parseInt(part));
        }
        return s;
    }

    public static void main(String[] args)
    {
        String arg0 = "\"1 2 3 4 5\"";
        String arg1 = "\"5 6 7 8 9\"";
        if(!isValidArgument(arg0))
            System.err.println("Invalid argument at index 0");
        if(!isValidArgument(arg1))
            System.err.println("Invalid argument at index 1");

        // merge into a set (to avoid duplicates)
        Set<Integer> mergedSet = toIntegerSet(arg0);
        mergedSet.addAll(toIntegerSet(arg1));

        // turn into a list (to sort)
        List<Integer> mergedList = new ArrayList<>(mergedSet);
        java.util.Collections.sort(mergedList);

        // turn into array (for displaying)
        Integer[] mergedArray = mergedList.toArray(new Integer[mergedList.size()]);
        System.out.println(java.util.Arrays.toString(mergedArray));
    }
}

Or check the live version on IdeOne

https://ideone.com/Ow2eEQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM