Two Laptop Bag*
The moose likes Beginning Java and the fly likes Using replace properly (word censor program) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using replace properly (word censor program)" Watch "Using replace properly (word censor program)" New topic
Author

Using replace properly (word censor program)

James Sands
Greenhorn

Joined: Aug 10, 2012
Posts: 15
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Feb 26, 2001
Posts: 4456
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
James Sands
Greenhorn

Joined: Aug 10, 2012
Posts: 15
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: Aug 10, 2012
Posts: 15
Where a little help gets you on CodeRanch!:



Thanks, without those little hints I wouldn't have got this far!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

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

Joined: Aug 10, 2012
Posts: 15
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

Joined: Oct 21, 2008
Posts: 1107

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using replace properly (word censor program)