This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using replace properly (word censor program)

 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, me again! - I just thought you could have a look through this - I like the whole "dont tell me the answer but learn" philosophy, so point me in the right direction.

My desired output to this compiling code is "p----", but as you can see I am frustratingly still getting "panda".

I think I'm not interpreting the String replace API correctly, but as I'm now getting frustrated - I will ask for a hand! Thank you!

 
Paul Clapham
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sands wrote:I think I'm not interpreting the String replace API correctly, but as I'm now getting frustrated - I will ask for a hand! Thank you!


Yes, where the API documentation says

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.


I think you missed the first four words.
 
Junilu Lacar
Bartender
Posts: 7482
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, Strings are immutable. That means they can never change. The replace method returns another String that is the result of replacing characters in the String that you called the replace method on. In other words, when you call pgr.replace(...), pgr does not change. You have to assign the result of replace() to something.

Another suggestion, while you're taking them: Try to give better variable names. When someone reads your code, do the names "x", "y", "pgr" help them make any sense of what's happening? It's like reading a sentence that goes something like this: "One dy, our grdnr, Bthlmw, dcded he wntd to mrry our tchr, Miss Lsbth." --- this is kind of hard to understand, right? Your code should tell the reader a story of what's happening. Well-written code is about telling people what's going on. The computer could care less because it knows what to do regardless of what name you give.
 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loving that - learning now learning!!

I see a stringbuilder replace method maybe better for me, I'll do some alterations and tell you how I get on!

Million thanks! James.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don’t like StringBuilder (which is a very useful class once you get used to it), remember you can get the individual chars out of the String as an array with one of the String class’ methods.
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Try to give better variable names.


One can however argue for short names for local variables of a short scope.
 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where a little help gets you on CodeRanch!:



Thanks, without those little hints I wouldn't have got this far!
 
Junilu Lacar
Bartender
Posts: 7482
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, that's a good start

Your code reads a lot better and I can actually make out the "story" you are trying to tell here. Now, like any good composition, it needs to be broken down into smaller chunks, like paragraphs and chapters and parts. See how you have everything in main()? This is where most people start but soon you'll learn that main() should only be like the preface of a story. The "meat" of the code should be in full-fledged objects, and different sections of the code should be assigned to different objects, each with their own responsibility. Not saying you have to do it now. You'll eventually realize this as you learn more about different object-oriented programming concepts. Good job and good luck!
 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people, well Junilu - your wish is my command. I have had a go. Good news, compiles and runs - Bad news I think it's a little messy. Especially with the way I create the player object into the arraystore class and then having to "double reference" my objects in main (e.g as.p.playerguess). Any thoughts or concepts I am missing to tidy up even further?

Thanks again, James.

 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
Junilu Lacar wrote:Try to give better variable names.


One can however argue for short names for local variables of a short scope.


Nope you can't the only exception is I for loop control
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic