RookieDeveloper
RookieDeveloper

Reputation: 75

Copying characters from a String in an array into a new array

I'm making a program to test whether or not strings in an array are palindromes.
I'm trying to take the strings from one array and take out any spaces or other characters so that it is only letters or digits.
Then taking the "clean" strings and storing them into a new array.
The error I get with this method is that it says that the left side of line 4 needs a variable, but I have it already declared as a String array.

Here is what I've got so far.

for (i = 0; i < dirty.length; i++) {
        for (int j = 0; j < dirty[i].length(); j++)
        if (Character.isLetterOrDigit(dirty[i].charAt(j))) {
            clean[i].charAt(j) = dirty[i].charAt(j);
        }
}

EDIT: the simplest solution I found was to create a temp String variable to add the characters one at a time depending if they were a letter or digit. Then convert to lowercase and then store into a string array. Here is the changed code that worked:

String clean[] = new String[i]; // Store the number of elements that the dirty array has that are not null

    for (i = 0; i < dirty.length; i++) {
        if (dirty[i] != null) // Only copy strings from dirty array if the value of the element at position i is not empty
        {
            for (int j = 0; j < dirty[i].length(); j++) {
                if (Character.isLetterOrDigit(dirty[i].charAt(j)))// take only letters and digits
                {
                    temp += dirty[i].charAt(j);// take the strings from the dirty array and store it into the temp variable

                }
            }
            temp = temp.toLowerCase(); // take all strings and convert them to lower case
            clean[i] = temp; // take the strings from the temp variable and store them into a new array
            temp = ""; // reset the temp variable so it has no value
        }
    }

Upvotes: 2

Views: 184

Answers (4)

Spotted
Spotted

Reputation: 4091

String clean = dirty.codePoints()
                    .filter(Character::isLetterOrDigit)
                    .collect(StringBuilder::new, 
                             StringBuilder::appendCodePoint,
                             StringBuilder::append)
                    .toString();

However, it would also be possible to use replaceAll with the appropriate regex to produce a new string containing only letters and digits.

Taken from here:

String clean = dirty.replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]", "");

Upvotes: 1

Adam
Adam

Reputation: 5599

String.charAt(i) just returns the char at given position. You can not assign new value to it. But you can change the String to an array of chars and then you can modify it as you want

char[] dirtyTab = dirty.toCharArray();

Upvotes: 0

MigSena
MigSena

Reputation: 218

String are immutable. You can use StringBuilder because it is not immutable and you can modify that. In you case you can use the void setCharAt(int index, char ch) function of the StringBuilder class.

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 192023

You cannot modify a StrIng. They are immutable.

You can, however, overwrite values in the array. Write a method to clean one string.

for (i = 0; i < dirty.length; i++) {
    dirty[i] = clean(dirty[i]);
}

Also its recommended you write a separate method to check for palindromes as well

Upvotes: 0

Related Questions