0

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?

1 Answer 1

1

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>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.