Search code examples
javajava.util.scanner

How to scan "arguments" from a String and leave out whitespaces?


I wanted to make the user type a command (For ex.: transact fromWallet toWallet 12.00 ) and then get the arguments without whitespaces out of the Scanner and pass them to variables to call specific methods with them. I'm still working on it but I managed to extract the arguments from the inputString without the methods that the Scanner provides. The Scanner was too problematic. It asked me to make 5 inputs, where I just needed 4 and so on. What is the "better way"? Is my solution okay? I ended up doing it this way (please tell me about "bad practices" in my code):

HELP METHODS IN THE CLASS:

public static String getNextArg(String s) {
    // get last char
    int cut = s.indexOf(" ");
    if (cut == -1)
        cut = s.length();

    // split arg
    String arg = s.substring(0, cut);

    return arg;
}

public static String getLeftOver(String s) {
    int cut = s.indexOf(" ");
    if (cut == -1)
        cut = s.length() - 1;

    s = s.substring(cut + 1);
    return s;
}

COMMAND PART IN THE MAIN:

  String cmd = "";
    String arg1, arg2, arg3, arg4;

    System.out.println("Type your command!");
    Scanner cmdScanner = new Scanner(System.in);
    do {
        cmd = cmdScanner.nextLine();

        // Check for "exit"
        if (cmd.length() >= 4)
            if(cmd.substring(0, 4).equalsIgnoreCase("exit"))
                System.exit(0); // Is this a good way to exit a program?

        System.out.println("The args passed to cmd are: ");
        if (!cmd.trim().isEmpty()) {
            arg1 = getNextArg(cmd);
            cmd = getLeftOver(cmd);
            System.out.println("arg1 = " + arg1);
        }
        if (!cmd.trim().isEmpty()) {
            arg2 = getNextArg(cmd);
            cmd = getLeftOver(cmd);
            System.out.println("arg2 = " + arg2);
        }
        if (!cmd.trim().isEmpty()) {
            arg3 = getNextArg(cmd);
            cmd = getLeftOver(cmd);
            System.out.println("arg3 = " + arg3);
        }
        if (!cmd.trim().isEmpty()) {
            arg4 = getNextArg(cmd);
            cmd = getLeftOver(cmd);
            System.out.println("arg4 = " + arg4);
        }
        System.out.println("Type your command!");
    } while (cmdScanner.hasNext());
}

The result fits my expectations. I just want to know how I could do this with the Scanner's next() methods. That could be helpful because later on I want to get the "next" BigDecimal in a proper way, and implementing own methods for everything would be a little bit to much for a noob like me.


Solution

  • public static void main(String... obj) {
        try (Scanner scan = new Scanner(System.in)) {
            final Pattern whiteSpace = Pattern.compile("\\s+");
            System.out.println("Type your command!");
    
            do {
                String[] args = whiteSpace.split(scan.nextLine().trim());
    
                if (args.length == 1 && "exit".equalsIgnoreCase(args[0]))
                    return;
    
                for (int i = 0; i < args.length; i++)
                    System.out.println("arg" + (i + 1) + " = " + args[i]);
    
                System.out.println("Type your command!");
            } while (scan.hasNext());
        }
    }