0

Im working on a project that can measure the distance between two cities. I am trying to set it so that the user will input a one line command to search for two cities, for example "distance Dallas New York City". Now I have it so that the line is split into an array of strings using the .split() method, however it splits on white space and punctuation, meaning that cities with names longer than one word won't work. Ideally I would like it to work so that typing "distance Dallas "New York City"" but I am not sure how to do this.

public static void main(String[] args) {
        CSVReader reader = new CSVReader();
        Scanner in = new Scanner(System.in);
        LinkedList[] entries = new LinkedList[14089];
        boolean running = true;

        reader.run("cities.csv", entries);

        while(running){
            String[] arg = in.nextLine().split("[,;:\\.\\s]+");

            if(arg[0].equals("retrieve")){
                search(arg[1], entries);
                System.out.println();
            }

            if(arg[0].equals("distance")){
                distance(arg[1], arg[2], entries);
                System.out.println();
            }

            if(arg[0].equals("stop")){
                stop(entries);
                running = false;
            }

        }
    }

}
Alan Moore
  • 68,531
  • 11
  • 88
  • 149
Hunter Tipton
  • 183
  • 1
  • 3
  • 11
  • What if I wanted to know the distance between a city named "Dallas New" and a city named "York City"? The command would still be `distance Dallas New York City`. You need to have a delimiter in your command. Something like, `distance dallas : new york city` – Rabbit Guy Apr 22 '16 at 18:14
  • @blahfunk That's not really what OP is asking. He wants to split his string into 3 parts, where the requirement is that if a city name has more than 1 word, it must be enclosed in quotes. He wants to know how to capture both cities like this. – Clark Kent Apr 22 '16 at 18:20
  • OP, if you would like to use `Matcher` and `Pattern` instead of split, there's an example of how to do this here: http://stackoverflow.com/a/7804472/1203882 – Clark Kent Apr 22 '16 at 18:23

2 Answers2

1

Maybe you can use this:

    String task = "distance: Dallas, New York City";
    System.out.println(task.split(":|,")[0]);// return "distance"
    System.out.println(task.split(":|,")[1]);// return " Dallas"
    System.out.println(task.split(":|,")[2]);// return " New York City"
J. Ordaz
  • 379
  • 5
  • 14
0

If you use a Pattern and Matcher from java.util.regex, you can use a regex like ".*?"|\S+ to tokenize the input, and then you can use quotes around multi-word cities. Regex101 Tested

First compile the regex at the start of the code:

final Pattern p = Pattern.compile("\".*?\"|\\S+");

And then generate arg like this:

String[] arg = new String[3];
Matcher m = p.matcher(in.nextLine());
for (int i = 0; i < arg.length && m.find(); i++) {
    arg[i] = m.group().replace("\"", "");
}
4castle
  • 28,713
  • 8
  • 60
  • 94