Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

While loop and cannot find symbol errors  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm writing some code for class that will take two strings and remove any instances of the 2nd string (plus the digit before) from the first. Most of is working fine as far as I can tell, but I'm currently running into 2 reoccurring errors:

The first error indicates that the way I'm attempting to create a loop which will continue as long as instances of "remove" exist is incorrect, but I'm not sure how else to phrase this? I do not understand what the issue is with my second error. Can anyone help me resolve these errors?

Full code is included below:


Thank you!
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understood correctly what are you trying to achieve here, I think you over-engineered it.

But...

Could you please give an example of input(s) and tell what the expected output(s) supposed to be, so that way will be easier to understand (at least for me), what is your goal.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for one of those erros if to follow error message blindly:

StringRemover.java:22: error: incompatible types: String cannot be converted to boolean
   while(cleaned.substring(remove))


it tells you that while () condition is expecting boolean value and not the String.
Expected:

You have (doesn't make sense):
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check String class's replaceAll() method whether it wouldn't be of use in your program.
 
Collie Mason
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much!

Could you please give an example of input(s) and tell what the expected output(s) supposed to be, so that way will be easier to understand (at least for me), what is your goal.


Simple Data:
xR-MxR-MHelloxR-M and R-M
(which should output "Hello")
fuxqwexqwertyxqwexqwertyrtyxqwertyrtyn and qwerty
(which would output "fun")


Addressing the above, how can I change my current code to fit that format?

Check String class's replaceAll() method whether it wouldn't be of use in your program.


The replaceAll() suggestion seems like it would work (and I appreciate the suggestion), but at a glance I feel like it would eliminate the need for the loop (which is what I'm trying to get practice with in this code). Am I misinterpreting this?
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Collie Mason wrote:Simple Data:
xR-MxR-MHelloxR-M and R-M
(which should output "Hello")


So how those "x"s get removed?
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Collie Mason wrote:The replaceAll() suggestion seems like it would work... but at a glance I feel like it would eliminate the need for the loop


Correct, and that is why standard Java set of libraries exist, to help developers to re-use solutions to the common problems and concentrate on the business logic, instead of trying to reinvent wheel.
 
Collie Mason
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry, I must've forgotten to clarify! The code should remove the letter before the instance of "remove" to be deleted as well as the instance itself. That should remove the "x"s in the example.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Collie Mason wrote:Oh sorry, I must've forgotten to clarify! The code should remove the letter before the instance of "remove" to be deleted as well as the instance itself. That should remove the "x"s in the example.


Ok, in which case replaceAll() won't help you much. Second input and output example also clarifies that.

 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Collie Mason wrote:remove any instances of the 2nd string (plus the digit before)


Collie Mason wrote:Simple Data:
xR-MxR-MHelloxR-M and R-M
(which should output "Hello")
fuxqwexqwertyxqwexqwertyrtyxqwertyrtyn and qwerty
(which would output "fun")


And where those digits are as I don't seem to see them?

Asking all these questions so all would have a better understanding with what kind of complex task we are dealing with.
 
Collie Mason
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's fine, I appreciate the help.
If I'm understanding your question correctly, the String of which all instances should be removed is the second ("R-M" in the first example), whereas the String which the instances are to be removed from is the first ("xR-MxR-MHelloxR-M"). The "digits" as I referred to them in-

remove any instances of the 2nd string (plus the digit before)


are the "x"s you previously asked about. Which I clarified here-

Oh sorry, I must've forgotten to clarify! The code should remove the letter before the instance of "remove" to be deleted as well as the instance itself. That should remove the "x"s in the example.

 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!