Question

I have some raw output that I want to clean up and make presentable but right now I go about it in a very ugly and cumbersome way, I wonder if anyone might know a clean and elegant way in which to perform the same operation.

    int size = charOutput.size();
    for (int i = size - 1; i >= 1; i--) 
    {
        if(charOutput.get(i).compareTo(charOutput.get(i - 1)) == 0) 
        {
            charOutput.remove(i);
        }
    }


    for(int x = 0; x < charOutput.size(); x++)
    {
        if(charOutput.get(x) == '?') 
        {
            charOutput.remove(x);
        }
    }


    String firstOne = Arrays.toString(charOutput.toArray());

    String secondOne = firstOne.replaceAll(",","");

    String thirdOne = secondOne.substring(1, secondOne.length() - 1);

    String output = thirdOne.replaceAll(" ","");


    return output;
Was it helpful?

Solution

ZouZou has the right code for fixing the final few calls in your code. I have some suggestions for the for loops. I hope I got them right...

These work after you get the String represented by charOutput, using a method such as the one suggested by ZouZou.

Your first block appears to remove all repeated letters. You can use a regular expression for that:

Pattern removeRepeats = Pattern.compile("(.)\\1{1,}");
// "(.)" creates a group that matches any character and puts it into a group
// "\\1" gets converted to "\1" which is a reference to the first group, i.e. the character that "(.)" matched
// "{1,}" means "one or more"
// So the overall effect is "one or more of a single character"

To use:

removeRepeats.matcher(s).replaceAll("$1");
// This creates a Matcher that matches the regex represented by removeRepeats to the contents of s, and replaces the parts of s that match the regex represented by removeRepeats with "$1", which is a reference to the first group captured (i.e. "(.)", which is the first character matched"

To remove the question mark, just do

Pattern removeQuestionMarks = Pattern.compile("\\?");
// Because "?" is a special symbol in regex, you have to escape it with a backslash
// But since backslashes are also a special symbol, you have to escape the backslash too.

And then to use, do the same thing as was done above except with replaceAll("");

And you're done!

If you really wanted to, you can combine a lot of regex into two super-regex expressions (and one normal regex expression):

Pattern p0 = Pattern.compile("(\\[|\\]|\\,| )"); // removes brackets, commas, and spaces
Pattern p1 = Pattern.compile("(.)\\1{1,}"); // Removes duplicate characters
Pattern p2 = Pattern.compile("\\?");

String removeArrayCharacters = p0.matcher(charOutput.toString()).replaceAll("");
String removeDuplicates = p1.matcher(removeArrayCharacters).replaceAll("$1");
return p2.matcher(removeDuplicates).replaceAll("");

OTHER TIPS

Use a StringBuilder and append each character you want, at the end just return myBuilder.toString();

Instead of this:

String firstOne = Arrays.toString(charOutput.toArray());
String secondOne = firstOne.replaceAll(",","");
String thirdOne = secondOne.substring(1, secondOne.length() - 1);
String output = thirdOne.replaceAll(" ","");
return output;

Simply do:

StringBuilder sb = new StringBuilder();
for(Character c : charOutput){
    sb.append(c);
}
return sb.toString();

Note that you are doing a lot of unnecessary work (by iterating through the list and removing some elements). What you can actually do is just iterate one time and then if the condition fullfits your requirements (the two adjacent characters are not the same and no question mark) then append it to the StringBuilder directly.

This task could also be a job for a regular expression.

If you don't want to use Regex try this version to remove consecutive characters and '?':

int size = charOutput.size();
if (size == 1) return Character.toString((Character)charOutput.get(0));
else if (size == 0) return null;

StringBuilder sb = new StringBuilder();
for (int i = 0; i < size - 1; i++) {
    Character temp = (Character)charOutput.get(i);
    if (!temp.equals(charOutput.get(i+1)) && !temp.equals('?'))
        sb.append(temp);
}

//for the last element
if (!charOutput.get(size-1).equals(charOutput.get(size-2)) 
        && !charOutput.get(size-1).equals('?'))
    sb.append(charOutput.get(size-1));

return sb.toString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top