Hi everyone - Having a little trouble with a Java assignment that I've been given, and was hoping I could get a little help here to get me moving again. I'm trying to develop a simple substitution cipher, and I'm stuck on the following:
I have a string (spaces and repeated chars removed. "hello world" is "helowrd". What I'm trying to do now is traverse the string, and add all unused letters of the alphabet to the end of that string, so "helowrd" would become "helowrdabcfgijkmnpqstuvxyz". I've got in to a confusion with String and StringBuilder, and what I try I just can't seem to do it. Here is my method as it stands:
And the error I get is heap space.
I've been googling for hours, really have been trying but haven't got anywhere, so any pointers in the right direction to get me moving would be great.
Thanks!!!
I have a string (spaces and repeated chars removed. "hello world" is "helowrd". What I'm trying to do now is traverse the string, and add all unused letters of the alphabet to the end of that string, so "helowrd" would become "helowrdabcfgijkmnpqstuvxyz". I've got in to a confusion with String and StringBuilder, and what I try I just can't seem to do it. Here is my method as it stands:
private static String formKey(String keyphrase) {
final String alphabet = "abcdefghijklmnopqrstuvwxyz";
String newKeyPhrase = keyphrase.replaceAll("\\s","");
char[] chars = newKeyPhrase.toCharArray();
Set<Character> charSet = new LinkedHashSet<Character>();
for(char c : chars) {
charSet.add(c);
}
StringBuilder key = new StringBuilder(26);
for(Character character : charSet) {
key.append(character);
}
for (char c = 'a'; c < 'z'; c++) {
for (int i = 0; i < key.length(); i++) {
if (!(key.charAt(i) == c)) {
key.append(c);
}
}
}
return key.toString();
}
And the error I get is heap space.
I've been googling for hours, really have been trying but haven't got anywhere, so any pointers in the right direction to get me moving would be great.
Thanks!!!