Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Replacing characters in a an array

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Replacing characters in a an array

    I just came across this post from the forum...is that the only way to code the JOptionPane? Seems rather similar to Sys.out

    BeginnerJOptionPane( input/output) homework problem. PLEASE HELP!



    --- Update ---
    I had more than this, but post was deleted somehow and this was the gist of it:

    WHERE THE HECK DID MY POST GO!!!!!! IS THERE A WAY TO RECOVER??????

    for(i = 0 ; i < charArray.length; i++){
    				if(Character.isDigit(charArray(i))){
    					address.replace("6", "*");
                                            address.replace("7", "*");

    Is there a better way to scan a character array for digits and replace any number in the array with another character ("*") in this case? The array is the address, " 1234 Runner Road " .
    Last edited by javaStooge; March 7th, 2014 at 08:28 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Replacing characters in a an array

    way to scan a character array for digits and replace
    That sounds like something that a regular expression could do if the contents of the array were in a String. I'm no good with regex so I'll leave it to someone that is.
    If the values are in an array, then a loop with some if statements would be a way to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Replacing characters in a an array

    Yes, that was the direction I was headed, except replace() accepts only replace(old char, new char), so I'm not sure how else to condense the code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Replacing characters in a an array

    Are you working with a String or an array?
    With a String I think a regex might work.
    With an array, a loop and if statement would do it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Replacing characters in a an array

    I'm not familiar with the regex....but this is what I was thinking, except I'm getting an error by using
     isDigit(charArray(i))
    Not sure how else to scan the character array.

    for(i = 0 ; i < charArray.length; i++){
    				if(Character.isDigit(charArray(i))){
    					address.replace("0", "*");
    					address.replace("1", "*");
    					address.replace("2", "*");
    					address.replace("3", "*");
    					address.replace("4", "*");
    					address.replace("5", "*");
    					address.replace("6", "*");
    					address.replace("7", "*");
    					address.replace("8", "*");
    					address.replace("9", "*");
    				}
    			}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Replacing characters in a an array

    If you are working with an array, you could use a loop, test each element of the array and change that element of the array by assigning it the new value. The code would not use the replace() method.

    If you are working with a String, the String class has methods that can scan the String and do replacements using a regex to select which characters to replace.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Replacing characters in a an array

    I need the program to process and scan the address and print an output stating whether the character is a digit or a letter.

    for(i = 0 ; i < charArray.length; i++){
    			if(Character.isDigit(i)){
    				System.out.println(i + "is a digit."); 
    			}
    			else if(Character.isLetter(i)){
    				System.out.println(i + "is a letter.");
    			}
    		}

    Nothing is printing though...

    I used a similar method for replace() ... which isn't printing out anything either. I don't understand why it isn't working. Replace is suppose to change the character and assign the new character we prescribe...but it does not.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Replacing characters in a an array

    The code is testing the value of i. That is the int that controls the for loop. It is NOT a char to be tested by the Character class's methods.
    Where is the char values that you want to test? Would i be an index into a char array?

    Replace is suppose to change the character and assign the new character we prescribe...but it does not.
    Read the API doc for the replace() method. It returns a String with the changes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Eliminating Unicode Characters and Escape Characters from String
    By bilalonjavaforum in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 19th, 2013, 04:26 AM
  2. How to replace characters in Array with user input Characters
    By gdoggson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 4th, 2013, 04:53 AM
  3. [SOLVED] Inserting characters from string into 2d array?
    By amf19 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 12th, 2013, 06:01 AM
  4. Replacing a JButton
    By dougie1809 in forum AWT / Java Swing
    Replies: 3
    Last Post: April 10th, 2013, 07:52 AM
  5. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM