Results 1 to 2 of 2
  1. #1
    nikki101 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default replacing characteter in string without using replace.

    //function will take string and replace all characters with *
    private static void changingThing(String theString, String chara)
    {
    String theNewString = "";
    String[] theStringArray = new String[] {theString};
    for (int i = 0; i < theString.length(); i++)
    {
    if (theStringArray[i] == "chara")
    theNewString = theNewString + '*';
    else
    theNewString = theNewString + theStringArray[i];
    }
    theString = theNewString;
    Scanner input = new Scanner(System.in);
    System.out.println("String with " + "\'" + chara + "\' " +
    "masked:");
    System.out.println(theString);
    }

    I am not sure where to start. I am a real beginner in java.
    Last edited by nikki101; 05-18-2013 at 05:47 AM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: replacing characteter in string without using replace.

    Your string array is length of one and contains only one string so you would get an ArrayIndexOutOfBounds exception. Check out the toCharArray method of the String class. If you are having problems, simply experiment with the methods to see what they do before you apply them to solving a problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replacing Image for a String
    By vimalaranjan in forum Java 2D
    Replies: 6
    Last Post: 05-04-2011, 07:09 PM
  2. Replacing char in string help
    By jimmy-lin in forum New To Java
    Replies: 3
    Last Post: 10-12-2009, 07:01 AM
  3. Replacing the chars within a string.
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 05:00 AM
  4. Help Replacing String
    By 7oclock in forum New To Java
    Replies: 5
    Last Post: 02-14-2009, 08:31 AM
  5. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 04:08 PM

Posting Permissions

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