• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need helps to solve the String manipulation

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to write an method that return the maximum number of consecutive blanks in the String parameter str1.

E.x: We have String: the method should return 3.

I take some time to try to solve the problem but ....no results.

Could you kindly give me some recomendations?

Thanks a lot

 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Change this line to
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you make that change, you'll notice that your logic is not quite correct. As a suggestion without simply telling you how to fix it, note that naming variables with "temp" is rarely informative. Since every variable can be changed (unless it's final), they're all pretty much temporary.

While scanning the String, at any point you have the maximum number of spaces found previously and the number of spaces found in the current run of spaces. Write down before coding what you should do when you find a space and when you find a non-space. Also, before you start scanning you know that the current maximum is zero, but what happens when you finish scanning?

Finally, you'll want to try multiple test Strings, taking extra care to come up with "corner cases": inputs that may trigger special cases in your logic. One obvious special case is a String with no spaces. Can you think of others?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic