2

I am trying to use the string after I passed it through my cleanUp function. When I return the arguments it no longer keeps the same value.

My initial strings have punctuations. I pass the strings to a method to clean up the punctuations. I return the modified strings back to my main method. I print out the modified string but my results have returned to the original value of the string.

public class test{

    public static void main(String[] args){

        String str1 = "this-is.first:sentence/.";
        String str2 = "this=is.second:sentece.";

        String[] arr = cleanUp(str1, str2);

        for (String string : arr){
            System.out.println("string after cleanup()" + string);
        }
    }

    public static String[] cleanUp(String str1, String str2) {
        String[] arr = {str1, str2};
        for (String string : arr){
            string = string.replaceAll("\\p{Punct}","");
            System.out.println("string cleaned:" + string);
        }
        return new String[] {str1, str2};
    }

}

Current Output:

string cleaned: this is first sentence  
string cleaned: this is second sentece 
string after cleanup(): this-is.first:sentence/.
string after cleanup(): this=is.second:sentece.

Expected Output:

string cleaned: this is first sentence  
string cleaned: this is second sentece 
string after cleanup(): this is first sentence
string after cleanup(): this is second sentece
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
bkapbkap
  • 23
  • 4
  • 2
    Your are just re-assigning the local variable `string` in the loop, not what is inside the array. Do something like `arr[i] = arr[i].replace(...)`, then it will work. Also, you are building your output out of `str1` and `str2`. But you never change those. You need to use whats inside the array instead! Or directly return your array. Java strings are immutable and java is pass by value. – Zabuzard Sep 01 '19 at 03:02

2 Answers2

1

You have two issues in your code which have to do with the fact that Java is pass-by-value.

  1. You are re-assigning your local string variable in the loop, not what is inside the array. So string has the correct value, the string in array still has the old value.
  2. Your output is build out of string1 and string2 which you did never update. Your updated content is supposed to be in the arr array you built using your loop. So you must either update the values based on the arrays content or return the arrays content or directly the array

Here is a fixed version:

public static String[] cleanUp(String str1, String str2) {
    String[] arr = { str1, str2 };
    for (int i = 0; i < arr.length; i++) {
        arr[i] = arr[i].replaceAll("\\p{Punct}", "");
        System.out.println("string cleaned:" + arr[i]);
    }
    return arr;
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 1
    Thank you Zabuza! I was able to come to the same solution with your earlier comment! Thank you for providing more details here! – bkapbkap Sep 01 '19 at 03:18
1

A Java String is immutable, and when you use the for-each loop it hides your iterator. Basically, your current code is almost correct but instead of using a for-each loop and locally modifying a temporary String, use a regular loop and modify the array you generate on the first line. Also, you should return that array instead of creating a new one. And, I assume you wanted to keep some white-space in your output. Something like,

public static String[] cleanUp(String str1, String str2) {
    String[] arr = { str1, str2 };
    for (int i = 0; i < arr.length; i++) {
        arr[i] = arr[i].replaceAll("\\p{Punct}", " ").replaceAll("\\s+", " ");
        System.out.println("string cleaned:" + arr[i]);
    }
    return arr;
}

Which I tested with your other code (and it returns)

string cleaned:this is first sentence 
string cleaned:this is second sentece 
string after cleanup()this is first sentence 
string after cleanup()this is second sentece 
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249