private String removeDuplicates(String userKeyword){
int wordLength = userKeyword.length();
int lengthCounter;
for (lengthCounter=0; lengthCounter<wordLength; lengthCounter++){
if (userKeyword.charAt(lengthCounter) != userKeyword.charAt(lengthCounter + 1)){
String revisedKeyword = "" + userKeyword.charAt(lengthCounter);
userKeyword = revisedKeyword;
}
}
return userKeyword;
}
I'm really new to java. We haven't used String builders, Strung buffer, Arrays, etc yet.... We haven't even gotten to loops, but I figured it will be the easiest way to use... Please help.
Following are my two favorite ways to remove duplicated chars in String:
The first one doesn't use StringBuffer or StringBuilder. Only using contains().
And if StringBuffer or StringBuilder is allowed, the code may become even cleaner and simpler. Here is my second way that uses StringBuilder.
Of course, using set to implement this function is also a good solution. Kon has done a great job in this thread.
Let's keep it clean and simple. The following code will check and make sure character held at
.charAt(char c)
is not found on theresult
String
.A simple solution would be
There are infinitely many ways to do this. Finding your own solution within the bounds of the toolset you've established is what learning to program is all about. Here is one possible solution to get you thinking:
Create a Set, which by default can only hold unique values
Then loop over the characters in your String, adding each to mySet
When you're done, your set will have a list of characters that are unique and in order, you can print them out with
EDIT:
Here is a simple set up using nested loops
I'm using a hard example to keep things simple. Please understand that the String
temp
will start empty, but when your whole process is done, your goal is to havetemp
have the same characters as einstein without duplicates. Something likestein
}
Now I haven't tested this so it's likely it has bugs, but walk through it mentally and try to understand it. Ask me when confused.
Well, I can see what approach you were trying to use. First of all, when you say "removing duplicates", are you only removing duplicates that are next to each other? In other words, you want to change "bookkeeper" to "bokeper" but "abcabcabc" doesn't get changed. If you want to return just "abc" for the second one, your whole approach is wrong.
Assuming you want to remove just the duplicates that are next to each other, you were sort of on the right track, but here's what you did wrong:
(1) When you go through looking at each character in userKeyword, like this:
you will get messed up if you change userKeyword inside the loop, as you did. The characters will shift over, but you'll keep incrementing the index, which means you will skip over some characters.
(2) Since you're looking at the characters at lengthCounter and lengthCounter+1, you have t be careful when you reach the end of the string. In this case, you don't want the index to reach the last character, because there is no character after it, and charAt(lengthCounter + 1) will crash. Change the
for
to saylengthCounter < wordLength-1
.(3) Finally, you're setting revisedKeyword and userKeyword to a one-character string.
This certainly can't be what you want. You probably want to set up a new String to hold the entire keyword. Something like this: put this outside the loop
and then, to add a new character to it
and don't change userKeyword inside the loop; then at the end, newKeyword will be the answer.
(I'm assuming you need to learn how to use loops and stuff. In real life, I think I could do the whole thing in one line with a regex, but that's not the point of the exercise.)