0

端末から入力を読み取ろうとしています。このために、BufferedReader を使用しています。これが私のコードです。

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(""));

私のコードのどこかにHashTable、キーと値が両方とも文字列である場所があります。問題は次のとおりです。

から値を取得したい場合、HashTable検索に使用しているキーHashTableは args 要素の 1 つです。これらの引数は奇妙です。ユーザーが 2 つの引数を入力すると (最初の引数はコマンドで、2 番目はユーザーが検索したいものです)、一致するものが見つかりません。

たとえば、HashTable に次の値が含まれているとします。

[ {"Hello":"World"}, {"One":"1"}, {"Two":"2"} ]

ユーザーは次のように入力します。

get Hello

私のコードは を返しません"World"

そこで、デバッガー (Eclipse を使用) を使用して、 の中身を確認しましたargsargs[1]が含まれていることがわかりましたが"Hello"、内部には値を持つargs[1]という名前のフィールドがあります。value['g','e','t',' ','H','e','l','l','o']

についても同様ですargs[0]。含まれています"get"が、フィールドvalueには['g','e','t',' ','H','e','l','l','o']!が含まれています。

なんてこったい!!!

ただし、 my をチェックするとHashTable、 key がどこにあるか"Hello"、 value= ['H','e','l','l','o'].

何か案は?

どうもありがとうございました。


編集:

これがコードサンプルです。同じことが今でも起こっています。

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"));

}
}

同じことが今でも起こっています。ハッシュテーブルを誤解しているに違いありません。物事がうまくいかないアイデアはありますか?

4

3 に答える 3

1

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?

于 2009-04-02T22:30:00.213 に答える
1

私は自分の間違いに気づきました。contains() の代わりにcontainsKey ()を使用する必要があります。

助けてくれたみんなに感謝したい。

おまけとして、「値」フィールドとは何かについても学びました。良い!

于 2009-04-03T00:23:32.063 に答える
0

Jon が言ったようにString、Java での s の内部実装のアーティファクトを見ているだけです。value目的に応じてフィールドを無視できます。それは、物事がうまくいかない理由とは何の関係もありません。動作していないコードの部分を示す必要があります。

于 2009-04-02T23:19:48.847 に答える