Reputation: 33
i want to replace my string with specific symbol using hashmap. But i cannot do this. How can i do?Please help This is my code....
public class MyImplement {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Map<String,Character> inputMap = new HashMap<String,Character>();
inputMap.put("a", '|');
inputMap.put("b", 'β');
inputMap.put("c", '⌐');
inputMap.put("d", '≡');
inputMap.put("e", '╨');
inputMap.put("f", 'Ω');
inputMap.put("g", '╟');
inputMap.put("h", '¬');
inputMap.put("i", '↔');
inputMap.put("j", 'Σ');
inputMap.put("k", '¥');
inputMap.put("l", '╒');
inputMap.put("m", '┼');
inputMap.put("n", '«');
inputMap.put("o", 'Φ');
inputMap.put("p", '╔');
inputMap.put("q", 'Є');
inputMap.put("r", '┴');
inputMap.put("s", 'δ');
inputMap.put("t", '╬');
inputMap.put("u", '┤');
inputMap.put("v", 'θ');
inputMap.put("w", '●');
inputMap.put("x", '◙');
inputMap.put("y", 'σ');
inputMap.put("z", '∞');
Scanner ins = new Scanner(System.in);
System.out.println("Enter a String");
String myData = ins.nextLine();
char arr[]=new char[myData.length()];
arr=myData.toCharArray();
for(int i = 0; i < arr.length; i++) {
arr[i]=inputMap.get(arr[i]);
System.out.println( arr[i]);
}
}
For example... if i enter string.... pop it will show like...╔Φ╔
How can i do this?
Upvotes: 0
Views: 98
Reputation: 680
The problem here is that your Map is created as
new HashMap<String, Character>();
So, in this case, the key is a String, and a Character will be returned.
Look at your code again:
char arr[]=new char[myData.length()];
arr=myData.toCharArray();
for(int i = 0; i < arr.length; i++) {
arr[i]=inputMap.get(arr[i]);
System.out.println( arr[i]);
}
Your array arr contains chars, NOT Strings. So, when you get from the input map, you will have some compile errors, because a char is not a String. Instead, you should declare your map as
new HashMap<Character, Character>();
Upvotes: 1