How i can swap value and key from hashmap and user give stringbuilder from keyboard and replace one word from stringbuilder with new value in java?Ι want to make a reverse trasnlation slang internet's dictionary.For example if I had given this string "laugh and lound " after tsanlation I want string "loI".I had create a hasmap which is my dictionary and contains this values{fyi", "for your information"); ("dae", "Does anyone else"); ("thx","thank you"); ("omg","Oh my god"); ("lol","laughing out lound");.Also if user givving input the string "laugh out and lound" take to oytput "lol".
My code is: package javaapplication6;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class 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);
}
}
but I have problem with for 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
It can not find the input which are match with reversedHashMap key.