<p>For parsing player commands, I've most often used the <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29" rel="noreferrer">split</a> method to split a string by delimiters and then to then just figure out the rest by a series of <code>if</code>s or <code>switch</code>es. What are some different ways of parsing strings in Java?</p>

[{'answer_id': 2969, 'author': 'Daniel Broekman', 'author_id': 124, 'author_profile': 'https://Stackoverflow.com/users/124', 'pm_score': 4, 'selected': False, 'text': '<p>I really like regular expressions. As long as the command strings are fairly simple, you can write a few regexes that could take a few pages of code to manually parse.</p>\n\n<p>I would suggest you check out <a href="http://www.regular-expressions.info" rel="noreferrer">http://www.regular-expressions.info</a> for a good intro to regexes, as well as specific examples for Java.</p>\n'}, {'answer_id': 2971, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': '<p>A simple string tokenizer on spaces should work, but there are really many ways you could do this.</p>\n\n<p>Here is an example using a tokenizer:</p>\n\n<pre><code>String command = "kick person";\nStringTokenizer tokens = new StringTokenizer(command);\nString action = null;\n\nif (tokens.hasMoreTokens()) {\n    action = tokens.nextToken();\n}\n\nif (action != null) {\n    doCommand(action, tokens);\n}\n</code></pre>\n\n<p>Then tokens can be further used for the arguments.  This all assumes no spaces are used in the arguments... so you might want to roll your own simple parsing mechanism (like getting the first whitespace and using text before as the action, or using a regular expression if you don\'t mind the speed hit), just abstract it out so it can be used anywhere.</p>\n'}, {'answer_id': 2977, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I would look at <a href="http://www.google.com/search?q=Zork+in+Java" rel="noreferrer">Java migrations</a> of <a href="http://en.wikipedia.org/wiki/Zork" rel="noreferrer">Zork</a>, and lean towards a simple <a href="http://en.wikipedia.org/wiki/Natural_language_processing" rel="noreferrer">Natural Language Processor</a> (driven either by tokenizing or regex) such as the following (from this link):</p>\n\n<pre>\n    public static boolean simpleNLP( String inputline, String keywords[])\n    {\n        int i;\n        int maxToken = keywords.length;\n        int to,from;\n        if( inputline.length() = inputline.length()) return false; // check for blank and empty lines\n        while( to >=0 )\n        {\n            to = inputline.indexOf(\' \',from);\n            if( to > 0){\n                lexed.addElement(inputline.substring(from,to));\n                from = to;\n                while( inputline.charAt(from) == \' \'\n                && from = keywords.length) { status = true; break;}\n            }\n        }\n        return status;\n    }\n</pre>\n\n<p>...</p>\n\n<p>Anything which gives a programmer a reason to look at Zork again is good in my book, just watch out for Grues.</p>\n\n<p>...</p>\n'}, {'answer_id': 2985, 'author': 'andrewrk', 'author_id': 432, 'author_profile': 'https://Stackoverflow.com/users/432', 'pm_score': 5, 'selected': True, 'text': '<p>I assume you\'re trying to make the command interface as forgiving as possible. If this is the case, I suggest you use an algorithm similar to this:</p>\n\n<ol>\n<li>Read in the string\n\n<ul>\n<li>Split the string into tokens</li>\n<li>Use a dictionary to convert synonyms to a common form </li>\n<li>For example, convert "hit", "punch", "strike", and "kick" all to "hit"</li>\n<li>Perform actions on an unordered, inclusive base</li>\n<li><strong>Unordered</strong> - "punch the monkey in the face" is the same thing as "the face in the monkey punch"</li>\n<li><strong>Inclusive</strong> - If the command is supposed to be "punch the monkey in the face" and they supply "punch monkey", you should check how many commands this matches. If only one command, do this action. It might even be a good idea to have command priorities, and even if there were even matches, it would perform the top action.</li>\n</ul></li>\n</ol>\n'}, {'answer_id': 3539, 'author': 'svrist', 'author_id': 86, 'author_profile': 'https://Stackoverflow.com/users/86', 'pm_score': 2, 'selected': False, 'text': '<p>@CodingTheWheel Heres your code, a bit clean up and through eclipse (<kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>f</kbd>) and the inserted back here :)</p>\n\n<p>Including the four spaces in front each line.</p>\n\n<pre><code>public static boolean simpleNLP(String inputline, String keywords[]) {\n    if (inputline.length() &lt; 1)\n        return false;\n\n    List&lt;String&gt; lexed = new ArrayList&lt;String&gt;(); \n    for (String ele : inputline.split(" ")) {\n        lexed.add(ele);\n    }\n\n\n    boolean status = false;\n    to = 0;\n    for (i = 0; i &lt; lexed.size(); i++) {\n        String s = (String) lexed.get(i);\n        if (s.equalsIgnoreCase(keywords[to])) {\n            to++;\n            if (to &gt;= keywords.length) {\n                status = true;\n                break;\n            }\n        }\n    }\n    return status;\n}\n</code></pre>\n'}, {'answer_id': 3565, 'author': 'Telcontar', 'author_id': 518, 'author_profile': 'https://Stackoverflow.com/users/518', 'pm_score': 1, 'selected': False, 'text': '<p>When the separator String for the command is allways the same String or char (like the ";") y recomend you use the StrinkTokenizer class:</p>\n\n<p><a href="http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html" rel="nofollow noreferrer">StringTokenizer</a></p>\n\n<p>but when the separator varies or is complex y recomend you to use the regular expresions, wich can be used by the String class itself, method split, since 1.4. It uses the Pattern class from the java.util.regex package</p>\n\n<p><a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html" rel="nofollow noreferrer">Pattern</a></p>\n'}, {'answer_id': 3616, 'author': 'bpapa', 'author_id': 543, 'author_profile': 'https://Stackoverflow.com/users/543', 'pm_score': 3, 'selected': False, 'text': "<p>Sun itself recommends staying away from StringTokenizer and using the String.spilt method instead. </p>\n\n<p>You'll also want to look at the Pattern class.</p>\n"}, {'answer_id': 36412, 'author': 'Bartosz Bierkowski', 'author_id': 3666, 'author_profile': 'https://Stackoverflow.com/users/3666', 'pm_score': 4, 'selected': False, 'text': '<p>Parsing manually is a lot of fun... at the beginning:) </p>\n\n<p>In practice if commands aren\'t very sophisticated you can treat them the same way as those used in command line interpreters. There\'s a list of libraries that you can use: <a href="http://java-source.net/open-source/command-line" rel="noreferrer">http://java-source.net/open-source/command-line</a>. I think you can start with <a href="http://commons.apache.org/cli" rel="noreferrer">apache commons CLI</a> or <a href="http://args4j.kohsuke.org/" rel="noreferrer">args4j</a> (uses annotations). They are well documented and really simple in use. They handle parsing automatically and the only thing you need to do is to read particular fields in an object.</p>\n\n<p>If you have more sophisticated commands, then maybe creating a formal grammar would be a better idea. There is a very good library with graphical editor, debugger and interpreter for grammars. It\'s called <a href="http://www.antlr.org/" rel="noreferrer">ANTLR</a> (and the editor <a href="http://www.antlr.org/works/index.html" rel="noreferrer">ANTLRWorks</a>) and it\'s free:) There are also some example grammars and tutorials.</p>\n'}, {'answer_id': 36531, 'author': 'SaM', 'author_id': 883, 'author_profile': 'https://Stackoverflow.com/users/883', 'pm_score': 2, 'selected': False, 'text': '<p>If this is to parse command lines I would suggest using <a href="http://commons.apache.org/cli/" rel="nofollow noreferrer">Commons Cli</a>.</p>\n\n<blockquote>\n  <p>The Apache Commons CLI library provides an API for processing command line interfaces. </p>\n</blockquote>\n'}, {'answer_id': 36549, 'author': 'John with waffle', 'author_id': 279, 'author_profile': 'https://Stackoverflow.com/users/279', 'pm_score': 3, 'selected': False, 'text': '<p>Another vote for ANTLR/ANTLRWorks.  If you create two versions of the file, one with the Java code for actually executing the commands, and one without (with just the grammar), then you have an executable specification of the language, which is great for testing, a boon for documentation, and a big timesaver if you ever decide to port it.</p>\n'}, {'answer_id': 37054, 'author': 'Alotor', 'author_id': 2937, 'author_profile': 'https://Stackoverflow.com/users/2937', 'pm_score': 2, 'selected': False, 'text': '<p>Try <a href="https://java.net/projects/javacc" rel="nofollow noreferrer">JavaCC</a> a parser generator for Java.</p>\n\n<p>It has a lot of features for interpreting languages, and it\'s well supported on Eclipse.</p>\n'}, {'answer_id': 73662, 'author': 'Scott Stanchfield', 'author_id': 12541, 'author_profile': 'https://Stackoverflow.com/users/12541', 'pm_score': 1, 'selected': False, 'text': '<p>If the language is dead simple like just </p>\n\n<p>VERB NOUN</p>\n\n<p>then splitting by hand works well.</p>\n\n<p>If it\'s more complex, you should really look into a tool like ANTLR or JavaCC.</p>\n\n<p>I\'ve got a tutorial on ANTLR (v2) at <a href="http://javadude.com/articles/antlrtut" rel="nofollow noreferrer">http://javadude.com/articles/antlrtut</a> which will give you an idea of how it works.</p>\n'}, {'answer_id': 3531547, 'author': 'Pierre Gardin', 'author_id': 332324, 'author_profile': 'https://Stackoverflow.com/users/332324', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://beust.com/jcommander/" rel="nofollow noreferrer">JCommander</a> seems quite good, although I have yet to test it.</p>\n'}, {'answer_id': 13539884, 'author': 'Pratik', 'author_id': 513340, 'author_profile': 'https://Stackoverflow.com/users/513340', 'pm_score': 1, 'selected': False, 'text': '<p>If your text contains some delimiters then you can your <code>split</code> method.<br>\nIf text contains irregular strings means different format in it then you must use <code>regular expressions</code>.</p>\n'}, {'answer_id': 23633702, 'author': 'shouyu', 'author_id': 2899548, 'author_profile': 'https://Stackoverflow.com/users/2899548', 'pm_score': 1, 'selected': False, 'text': '<p>split method can split a string into an array of the specified substring expression <code>regex</code>.\nIts arguments in two forms, namely: split (<code>String regex</code>) and split (<code>String regex, int limit</code>), which split (<code>String regex</code>) is actually by calling split (String regex, int limit) to achieve, <em>limit is 0</em>. Then, when the <em>limit> 0</em> and <em>limit &lt;0</em> represents what? </p>\n\n<p>When the <strong>jdk</strong> explained: when <em>limit> 0</em> sub-array lengths up to limit, that is, if possible, can be <em>limit-1</em> sub-division, remaining as a substring (except by limit-1 times the character has string split end); </p>\n\n<p><em>limit &lt;0</em> indicates no limit on the length of the array; </p>\n\n<p><em>limit = 0</em> end of the string empty string will be truncated. \n<code>StringTokenizer</code> class is for compatibility reasons and is preserved legacy class, so we should try to use the split method of the String class.\nrefer to <a href="http://codingkit.com/java/9529.html" rel="nofollow">link</a></p>\n'}]
['https://Stackoverflow.com/questions/2968', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/362/']