Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble handling input in Java
    primarykey
    data
    text
    <p>I'm trying to read input from the terminal. For this, I'm using a BufferedReader. Here's my code.</p> <pre><code>BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] args; do { System.out.println(stateManager.currentState().toString()); System.out.print("&gt; "); args = reader.readLine().split(" "); // user inputs "Hello World" } while (args[0].equals("")); </code></pre> <p>Somewhere else in my code I have a <code>HashTable</code> where the Key and Values are both Strings. Here's the problem:</p> <p>If I want to get a value from the <code>HashTable</code> where the key I'm using to look up in the <code>HashTable</code> is one of the args elements. These args are weird. If the user enters two arguments (the first one is a command, the second is what the user wants to look up) I can't find a match.</p> <p>For example, if the HashTable contains the following values:</p> <pre><code>[ {"Hello":"World"}, {"One":"1"}, {"Two":"2"} ] </code></pre> <p>and the user enters:</p> <pre><code>get Hello </code></pre> <p>My code doesn't return <code>"World"</code>.</p> <p>So I used the debugger (using Eclipse) to see what's inside of <code>args</code>. I found that <code>args[1]</code> contains <code>"Hello"</code> but inside <code>args[1]</code> there is a field named <code>value</code> which has the value <code>['g','e','t',' ','H','e','l','l','o']</code>.</p> <p>The same goes for <code>args[0]</code>. It contains <code>"get"</code> but the field <code>value</code> contains <code>['g','e','t',' ','H','e','l','l','o']</code>!</p> <p>What the hell!!!</p> <p>However, if I check my <code>HashTable</code>, wherever the key is <code>"Hello"</code>, the value=<code>['H','e','l','l','o']</code>.</p> <p>Any ideas?</p> <p>Thank you very much.</p> <hr> <p>EDIT:</p> <p>Here's come code sample. The same is still happening.</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Hashtable; public class InputTest { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); Hashtable&lt;String, String&gt; EngToSpa = new Hashtable&lt;String, String&gt;(); // Adding some elements to the Eng-Spa dictionary EngToSpa.put("Hello", "Hola"); EngToSpa.put("Science", "Ciencia"); EngToSpa.put("Red", "Rojo"); // Reads input. We are interested in everything after the first argument do { System.out.print("&gt; "); try { args = reader.readLine().trim().split("\\s"); } catch (IOException e) { e.printStackTrace(); } } while (args[0].equals("") || args.length &lt; 2); // ^ We don't want empty input or less than 2 args. // Lets go get something in the dictionary System.out.println("Testing arguments"); if (!EngToSpa.contains(args[1])) System.out.println("Word not found!"); else System.out.println(EngToSpa.get(args[1])); // Now we are testing the word "Hello" directly System.out.println("Testing 'Hello'"); if (!EngToSpa.contains("Hello")) System.out.println("Word not found!"); else System.out.println(EngToSpa.get("Hello")); } } </code></pre> <p>The same is still happening. I must be misunderstanding Hash Tables. Ideas where things are going wrong?</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.
 

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