0
while (sentence.indexOf(lookFor) > lookFor)
{
    sentence += sentence.substring(sentence.indexOf(lookFor));
}
String cleaned = sentence;
return cleaned;

This is what I have tried to do in order to remove letters. lookFor is a char that was put in already, and sentence is the original sentence string that was put in already. Currently, my code outputs the sentence without doing anything to it.
EX Correct Output: inputting "abababa" sentence; char as "a" --->outputting "bbb" inputting "xyxyxy" sentence; char "a" ---> outputting "xyxyxy"

6
  • 3
    sentence.indexOf(lookFor) > lookFor You're comparing the position of the character in a string to the character itself. That doesn't make any sense. Commented Oct 13, 2015 at 21:18
  • sentence += ... You'll never remove anything from sentence this way. It will only get longer. Commented Oct 13, 2015 at 21:19
  • Do you want to remove all "a"? Commented Oct 13, 2015 at 21:19
  • Why don't you show us the whole function, with input parameters, return value, and samples of expected input/output? Commented Oct 13, 2015 at 21:19
  • what you need to do is print out each part of the code. print out sentence.indexOf(lookFor) before you use it. Commented Oct 13, 2015 at 21:21

3 Answers 3

4

You don't need while for a single string. Only if you read a text line after line. In your case something like

String a = "abababa";
a = a.replace("a",""); 

would give you the output "bbb"

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice, but for this assignment, I have to use a while loop to solve this. I am also inputting several strings. Sorry for not making that clear.
0

it probably isn't entering the loop at all.

sentence.indexOf(lookFor) is going to return the place of the character in the string.

lookFor is a char value. A value of 'a' has a numeric value of 97 so the while will only find things after the first 97 characters.

If your code ever entered the loop it would never return.

the substring command you are calling will take the found item to the end of the string.

+=, if it did what you think will append it to itself. so it will take 'ababab' and make it 'abababababab', forever. but luckily you can't use += on a string in java.

What you want is:

String something = "abababab";
something = something.replaceAll("a", "");

3 Comments

Actually your answer is a combination from the comments and my answer;)
It is always a good idea to avoid replaceAll if you're not using a (real) regular expression.
@JürgenK. hah. when I started typing there were no comments or answers. When I finished there were a ton. Such an easy question that happens. I up-voted your answer. and Tom, yeah you're right but I figured an example with a replace would just generate a question about replacing a non-char value.
-1

If you just need to get rid of letters use the replace method that others have written, but if you want to use a while loop, based on what I've seen of your logic, this is how you'd do it.

while (sentence.indexOf(lookFor) == 0)
    sentence = sentence.substring(1);
while (sentence.indexOf(lookFor) > 0)
{
    sentence = sentence.substring(0, sentence.indexOf(lookFor)-1)+
               sentence.substring(sentence.indexOf(lookFor)+1);
}
return sentence;

6 Comments

This create 3 String object for each loop iteration, while trashing the old one. Your garbage collection will sky rocket if applied to a text.
There are obviously better ways accomplish OP's task, but based on what we've seen of his logic, this is probably what he was trying to do. Sorry but he never gave any restrictions on garbage collection
Thank you for this! This worked out for me, but I'm not understanding the bit where the substrings are added together.
Ok so if someone ask you how to use a gun on is temple, you'll show him exactly how to do it? Op obviously has no clue about garbage collection. Teach him instead of giving him bad advices.
@anlam456 where you add the substrings is concatenation. sentence.substring(0, sentence.indexOf(lookFor)-1) gives you the substring of the entire string up to the letter you want to get rid of; sentence.substring(sentence.indexOf(lookFor)+1) gives you everything after the letter you want to get rid of
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.