This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need helps to solve the String manipulation

 
Khong Biet Gi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Change this line to
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic