|
-
September 11th, 2009, 01:39 PM
#4
Re: Need help eliminating multiples letters
I came up with the following, but I need some help with the logic. I think the problems lie in two main areas:
1. If you delete an item, the size will change but won't be able to update the loop.
2. Even if I do a test run with a word that has no repeating letters, such as "hey", where the length will definitely not change, it still prints out an incorrect statement.
Code:
public void removeMultiples(String x) {
String keyword = x;
StringBuffer sb, temp;
sb = new StringBuffer(keyword);
// reverses the keyword so that multiple letters are removed
// from the end, not the beginning
temp = sb.reverse();
for (int i = 0; i < temp.length(); i++) {
for (int j = 1; j < temp.length(); j++) {
char y = temp.charAt(i);
char z = temp.charAt(j);
// if what is at location y and z match, remove what is at z
if(y == z){
temp.deleteCharAt(j);
}
}
}
// corrects the orientation of the keyword
sb = temp.reverse();
System.out.println(sb);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|