0

I am trying to implement a way for taking in arguments for a photo album that I am building. However, I am having a hard time figuring out how to tokenize the input.

Two sample inputs:

addPhoto "DSC_017.jpg" "DSC_017" "Fall colors"

addPhoto "DSC_018.jpg" "DSC_018" "Colorado Springs"

I would like this input to return a String array containing 4 elements where

  • String s[1]="addPhoto"
  • String s[2]="DSC_017.jpg"
  • String s[3]="DSC_017"
  • String s[4] = "Fall colors"

I looked into StringTokenizer and String.split but I'm not sure how to go about setting the delimiters.

Petrov
  • 87
  • 1
  • 1
  • 8
  • possible duplicate of [Tokenizing a String but ignoring delimiters within quotes](http://stackoverflow.com/questions/3366281/tokenizing-a-string-but-ignoring-delimiters-within-quotes) – Barett Mar 05 '15 at 20:25

2 Answers2

1
    String line = "addPhoto \"DSC_018.jpg\" \"DSC_018\" \"Colorado Springs\"";
    String[] pieces = line.split(" \"");

    for (String p : pieces) {
        System.out.println(p.replaceAll("\"", ""));
    }
Efe Kahraman
  • 1,378
  • 13
  • 21
0

You might want to pull these arguments off the command line args, the shell will do the quote handling for you. However, you'll only be able to do one addPhoto operation at a time.

If you can't do that, you might try one of these answers:

Community
  • 1
  • 1
Barett
  • 5,226
  • 6
  • 46
  • 53