Deleting Characters
Author |
Message |
mrfungu
|
Posted: Tue May 13, 2008 8:52 pm Post subject: Deleting Characters |
|
|
Hi,
I am creating a program that asks the user for a string and then deletes a single character (specified by the user) from that string. In case the character occurs more than once, the program also asks for the occurrence of the character (first time occurring? second? etc.). The only method I can use are charAt() and length(). So far, I have been able to delete the character, when it only occurs once in the string. Any ideas on how to delete it if it occurs more than once? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Tue May 13, 2008 9:02 pm Post subject: RE:Deleting Characters |
|
|
well, i dont really get why and what the "occurrence" is for, but since you can only use the 2 methods, charAt() and length()
a easy way to do it is, dont even bother thinking about deleting a character, because with those 2 method, you cannot just "delete", and so you have to make a new string, and if the current letter is the letter they want to "delete" then lets not include that in our new string
and finally we got a string thats without the letter they wanted.
edit: i feel like i gave away too much..... ![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue May 13, 2008 10:12 pm Post subject: RE:Deleting Characters |
|
|
Small goals. Show us a method that takes as input a string and a character. It should print out the position of every occurrence of that character in the string. |
|
|
|
|
![](images/spacer.gif) |
mrfungu
|
Posted: Wed May 14, 2008 7:12 am Post subject: Re: Deleting Characters |
|
|
I already did the whole not including the character in the new string, but the problem is if the character occurs twice (like in hairy hat) the output ends up being airhat hairyat. It takes away both h's. That's where the occurrence comes in. If the user says "1" only the first h will be taken away, if they say "2", only the second one will be taken away. Thats where I'm having problems. |
|
|
|
|
![](images/spacer.gif) |
Reality Check
|
Posted: Sun May 18, 2008 3:22 pm Post subject: Re: Deleting Characters |
|
|
This is fairly simple. If you simply want to remove every appearance of a specified letter with only charAt() and length methods then you need to think of the exact things that these methods do. There are classes such as StringTokenizer that make this possible as well but think of what charAt and length do. Approach your question logically and think of what you would do in your head to solve this problem and transfer your process to the computer. I don't want to give away too much but let me know if you need more help. |
|
|
|
|
![](images/spacer.gif) |
|
|