Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A good place to start would be a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html" rel="nofollow">HashMap</a> and <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29" rel="nofollow">String.split</a>.</p> <p>You might try something like this:</p> <pre><code>// In main function HashMap&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;(); for (String arg : args) { String[] split = arg.split(Pattern.quote("=")); if (split.length &gt; 1) { // It had an equals sign. String var = split[0], value = split[1]; map.put(var, Integer.parseInt(value)); System.out.println(var + " is now " + value); } } </code></pre> <p>This won't account for <code>A+B</code>, but I'll leave that as a puzzle to you.</p> <p><em>Hint</em>: <a href="http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#containsKey%28java.lang.Object%29" rel="nofollow">map.containsKey</a>, <a href="http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#get%28java.lang.Object%29" rel="nofollow">map.get</a>.</p> <hr> <p><strong>EDIT</strong></p> <p>To answer your questions:</p> <pre><code>for (int i=0; i&lt;args.length; i++) { String arg = args[i]; // do something with arg } </code></pre> <p>is the same as </p> <pre><code>for (String arg : args) { // do something with arg } </code></pre> <p>The latter is called "for-each" or "for-in." The <code>args</code> variable contains the command-line arguments. I'm assuming that this code is in the main method, which should have a method signature of <code>public static void main(</code><strong><code>String[] args</code></strong><code>)</code>.</p> <p>The <code>put</code> method adds the key/value pair to the HashMap. Basically, a hashmap is an association (map) of keys to values. Each key can have one value, and <code>put</code> overwrites, so if you run</p> <pre><code>map.put("a", 1); map.put("b", 2); map.put("a", 3); </code></pre> <p>then the following statements are true:</p> <pre><code>map.get("a") == 3 map.get("b") == 2 map.get("c") == null map.get("asdf") == null </code></pre> <p>For more info, check out the <a href="http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html" rel="nofollow">map tutorial</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload