tags:

views:

138

answers:

3

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?

+1  A: 

Don't worry about the value field - that's just saying that there's a single char array containing the text of "get Hello", and both args[0] and args[1] refer to that char array, but they'll have different offsets and counts. args[0] will have an offset of 0 and a count of 3; args[1] will have an offset of 4 and a count of 5.

I've no idea why your hash map wouldn't be working though... can you provide a short but complete example program?

Jon Skeet
A: 

As Jon said, you're just seeing an artifact of the internal implementation of Strings in Java. You can ignore the value field for your purposes. That has nothing to do with why things aren't working for you. You need to show us the part of the code that's not working.

Eddie
+1  A: 

I just noticed my mistake. I have to use containsKey() instead of contains().

I'd like to thank everyone for helping.

As a bonus, I also learned about what the 'value' field is. Nice!

Sparragus