I'm trying to read input from the terminal. For this, I'm using a BufferedReader. Here's my code.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] args;
do {
System.out.println(stateManager.currentState().toString());
System.out.print("> ");
args = reader.readLine().split(" ");
// user inputs "Hello World"
} while (args[0].equals(""));
Somewhere else in my code I have a HashTable
where the Key and Values are both Strings. Here's the problem:
If I want to get a value from the HashTable
where the key I'm using to look up in the HashTable
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.
For example, if the HashTable contains the following values:
[ {"Hello":"World"}, {"One":"1"}, {"Two":"2"} ]
and the user enters:
get Hello
My code doesn't return "World"
.
So I used the debugger (using Eclipse) to see what's inside of args
. I found that args[1]
contains "Hello"
but inside args[1]
there is a field named value
which has the value ['g','e','t',' ','H','e','l','l','o']
.
The same goes for args[0]
. It contains "get"
but the field value
contains ['g','e','t',' ','H','e','l','l','o']
!
What the hell!!!
However, if I check my HashTable
, wherever the key is "Hello"
, the value=['H','e','l','l','o']
.
Any ideas?
Thank you very much.
EDIT:
Here's come code sample. The same is still happening.
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<String, String> EngToSpa = new Hashtable<String, String>();
// 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("> ");
try
{
args = reader.readLine().trim().split("\\s");
} catch (IOException e)
{
e.printStackTrace();
}
} while (args[0].equals("") || args.length < 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"));
}
}
The same is still happening. I must be misunderstanding Hash Tables. Ideas where things are going wrong?