Results 1 to 3 of 3
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Using a replace method...

    Hi, I am writing a java code and I need help with replacing the word I am starting off with into asterisks without using a while method.

    So for example:

    enter a word -> hello

    **
    So how can I go from "hello" to "**" using a replace method? Thanks

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Please explain more clearly. I did not understand what you want to do..

  3. #3
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

    I don't quite understand your question, but I'll give some advice.

    Java Code:
    String word = hello;
    
    for(int i = 0; i < word.length; i++)
    {
       word.replace(word[i], "*");
    }
    If my code above is syntactically correct, that will replace all letters in "word" with *. But since you've changed "hello" to "*****" you've lost the secret word completely. Maybe you want something like...

    Java Code:
    String word = hello;
    String coverUpWord = '';
    
    for(int i = 0; i < word.length; i++)
    {
       coverUpWord += '*';
    }
    Now both the above use for-loops not while-loops, but they are essentially the same. Why is it that you can't use a while-loop?

    Greetings.

Similar Threads

  1. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 06:51 AM
  2. Find and Replace in JTable
    By look4 in forum AWT / Swing
    Replies: 2
    Last Post: 11-24-2007, 01:27 AM
  3. problems with replace method
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 10:11 PM
  4. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 09:14 PM
  5. Using Find And Replace In Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 11:15 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
  •