For an assignment in my compsci class, I need to use hashmaps to convert a message into an encrypted message. We're told to do this by replacing all characters in a given message string with characters in a substitution alphabet that consists of lowercase letters from a to z. Also, the substitution should be done in alphabetical order.
So for example if we're given the message "hello", our cipher should map h to a, e to b, l to c, and o to d, giving us the string abccd. In theory, it doesn't seem that hard, but I'm having a lot of trouble with the actual conversion of characters. I've put a comment where I was intending to do the actual conversions, but so far all I'm getting returned is a bunch of a's and b's put together. What am I doing wrong here?
Thanks in advance for any help.
EDIT: Removed some redundant code thanks to @taha
public class Encryption {
public String encrypt(String message) {
StringBuilder convertedMessage = new StringBuilder();
int counter = 0;
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
HashMap<Character, Character> converter = new HashMap<>();
String messageholder = message.toLowerCase();
char[] charMessage = messageholder.toCharArray();
//this statement is what's giving me an aneurysm
for (char i : charMessage ) {
converter.putIfAbsent(i, alphabet[counter]);
counter++;
}
for (Character c : converter.keySet()) {
char value = converter.get(c);
convertedMessage.append(value);
}
return convertedMessage.toString();
}
}