Results 1 to 5 of 5
  1. #1
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default Replace String with "*"

    Say I have the word 'Mississippi'. The program I'm writing basically does asks the user for a number, and if that number is greater than 4, the given word (Mississippi) becomes masked with '*', so in this case, '***********'. But, if the user enters 3, then the given word is only masked up to every fourth letter, so for example, in this case, '***s***i***'. Now, how can I write code to mask in this way (only masking some of the text and not all)? I figured out how to mask them all for 4, that's pretty easy, but should I use a loop for 3? I'm not sure how to begin..

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I would suggest using a for loop to iterate through all the characters. The % operator would also help.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    Yes, looks like a loop is needed.
    Use a paper and pencil to work out the steps need to do the job, Look at how you did it and then design a program to do it.

  4. #4
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Yes, looks like a loop is needed.
    Use a paper and pencil to work out the steps need to do the job, Look at how you did it and then design a program to do it.
    Alright, I'm thinking that I can loop through and get every fourth character, so that way, I have all the characters that I need to print, but how can I replace the rest with '*'?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    but how can I replace the rest with '*
    Put the String into a StringBuffer or char[] so you can access and replace them using an index.

Similar Threads

  1. string comparison with "=" and ".equal"
    By guavajuice in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 10:01 PM
  2. Replies: 0
    Last Post: 04-02-2009, 08:02 PM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replace "\" with "/"
    By heartysnowy in forum New To Java
    Replies: 4
    Last Post: 11-20-2008, 05:16 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •