• 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

Using replace properly (word censor program)

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic