dimanche 20 septembre 2020

Replace character in a String with HashMap

I have some idea to do this

I'm stuck on the if statement part, how can I make the comparison work?

The expected output for this example is: The string ones is use two test the map numberthree.

      String s = "The string 1s is use 2 test the map number3.";
 
        HashMap<Integer,String> map = new HashMap<>();
        map.put(1,"one");
        map.put(2,"two");
        map.put(3,"three");
  
        for (int i = 0; i < s.length(); i++){
        if (s.charAt(i) is equals to map.contains(1){
           s[i] = map.get(1);
     }
}

Aucun commentaire:

Enregistrer un commentaire