2017-11-02 7 views
-5

ハッシュマップから値とキーを入れ替える方法と、ユーザーがキーボードからstringbuilderを出し、stringbuilderの1ワードをjavaの新しい値に置き換えることはできますか?Ι逆方向変換スラングインターネットの辞書を作成したい私はこの文字列にtsanlationの後に "笑い声"を与えました。文字列 "loI"が必要です。私の辞書であり、この値{fyi "、"あなたの情報 "}を含むhasmapを作成しました; ( "笑"、 "笑い声");また、ユーザーが "笑い声"を入力するとアウトとlound "笑」oytputために取る"ハーフマップからの値とキーのスワップ

私のコードは次のとおりです。 パッケージjavaapplication6;

パブリッククラスJavaApplication6 {

/** 
* @param args the command line arguments 
*/ 

public static void main(String[] args) { 



         String threeWords; 

         //create stringbuilder 
         StringBuilder acronym = new StringBuilder(); 

         Scanner scan = new Scanner(System.in); 

         //o xrhsths dinei eisodo thn protash poy thelei na metafrasei 
         System.out.println("Eisigage thn protasi sou pou theleis na metafraseis:\n "); 
         threeWords = scan.nextLine(); 
         threeWords = threeWords.toLowerCase(); //Changing it to lower case 
          acronym.append(threeWords); 
          System.out.println(" To string pou eisigages einai: " + acronym +"\n"); 
         //dhmiougroum ena pinaka pou periexei mia mia thn lejei jexorista apo thn protash pou 
         //edose o xrhsths oste na mporoume na broume pio eukola thn leji pou einai gia metafrash sto 
         //lejiko 
         String[] threeWordsArray = threeWords.split(" "); 

         //dhmiougria lejikou me thn slang tou internet me thn xrisi hashmap 
         HashMap<String,String> dictionary = new HashMap <String,String>(10); 

       //Prosthiki lejeon sto lejiko mas 

         dictionary.put("fyi", "for your information"); 
         dictionary.put("dae", "Does anyone else"); 
         dictionary.put("thx","thank you"); 
         dictionary.put("omg","Oh my god"); 
         dictionary.put("lol","laughing out lound"); 



         HashMap<String, String> reversedHashMap = new HashMap<String, String>(); 
           for (String key : dictionary.keySet()){ 
           reversedHashMap.put(dictionary.get(key), key); 
           } 


          System.out.println("reversedHasmap"+reversedHashMap); 
          for(String word : threeWordsArray) { 


          if (reversedHashMap.containsKey(word)) { 

          String definition = reversedHashMap.get(word); 
      System.out.println("h metafrash einai: \n" + definition); 

          int index=acronym.indexOf(word); 
          System.out.println("index:"+index); 
          acronym.insert(index,definition); 
         } 
      else { 
      System.err.println("Word not found"); 

          }//end if 

         }//end for 

         System.out.println("To string metafrasmeno einai: " + acronym); 





} 

}

が、私は問題を持っている(文字列ワード:threeWordsArray)用 用{

      if (reversedHashMap.containsKey(word)) { 

          String definition = reversedHashMap.get(word); 
      System.out.println("h metafrash einai: \n" + definition); 

          int index=acronym.indexOf(word); 
          System.out.println("index:"+index); 
          acronym.insert(index,definition); 
         } 
      else { 
      System.err.println("Word not found"); 

          }//end if 

         }//end for 

これは、ある入力を見つけることができませんreverseHashMapキーと一致します。

+2

あなたが望むのは正確に何ですか、わかりません?これまで何をしていますか? – Asew

+2

何ですか?いくつかの入力+出力とこれまでに試したことを示してください。 – luk2302

+0

StringBuilderがキーボードを使用している間は、キーを戻してからキーを戻す前に値を配置します。簡単です! – Michael

答えて

0

のHashMapには重複値

HashMap<Object, Object> given = new HashMap<>(); 
HashMap<Object, Object> result = new HashMap<>(); 
for (String i : ex.keySet()){ 
    result.put(given.get(i), i); 
} 

resultが存在しないと仮定すると、givenからスワップキーと値とのHashMapであろう。

1

オブジェクト(型)を保持する任意のマップ(mapToConvert)をループすることができます。これらのタイプは、Strings、Integerなどとなります。

エントリセット内の各エントリをループすると、entry.getKey()のキーとentry.getValue()のペアの値を同時に引き出すことができます。次に、値をキーとして、キーを値として使用して、新しいマップを作成することができます。

HashMap<Type,Type> newMap = new HashMap<>(); 
for(Map<Type,Type> entry : mapToConvert.entrySet()){ 
    Type key = entry.getKey(); 
    Type values = entry.getValue(); 
    newMap.put(values, key); 
} 

編集:分かりやすくするためです。

+0

私は少しの説明を追加しましたが、 @TobySpeightの推薦で。さらに説明できることがあれば、私に聞いてください。 – Sam

+0

はい私は私のプログラマのためにこれを書いているが、入力がreservesdhasmapキーと同じであるかどうかをチェックするための問題がある。入力キーボードがrevesed hasmap.Ifを持つ新しいキーと同じになるまでsthが見つからない私の投稿を編集すれば、私が書いた場所に自分のコードが見えます。 – waterfall100

+0

正確に一致するものを探す 'containsKey'を使っているというのが問題だと思います。例えば「大声で笑う」があなたの鍵になり、あなたはそのフレーズを分けているので、あなたは鍵の「笑い」を探しています。キーセットをループして、あなたの単語の1つを含むキーを探してください。 – Sam

関連する問題