Sylwia
Sylwia

Reputation: 7

Method that corrects the text, inserting spaces after the comma in Java

I want to create a refactorSeparators method that takes an object of the String type as an argument. Method returns the text from the input object corrected so that if there is any a comma or a period and there is no space after it, it will insert this space.I'm stuck, don't know what to do next, below is my code. How can I finish it? I wonder how to write this part: if (s.equals(".") && i.next().equals())

public class Separator {
public static void main(String[] args) {
    String text = "Periods,hyphens, the last two characters cannot be a period. The rest of them don't. And there you go.";

    ArrayList<String> stringArr = new ArrayList<>();

    String[] arrOfStr = text.split("");
    Iterator i = stringArr.iterator();


    for (String s : arrOfStr) {
        stringArr.add(s);
         System.out.println("{" +s + "}");
    }
    for (String s : arrOfStr) {
        if (s.equals(".") && i.next().equals()) {
            String space = " ";
            stringArr.add(i.next(, " ");


        } else {
            System.out.println("error");

        }

    }
}}

Upvotes: 0

Views: 155

Answers (3)

Thilo Schwarz
Thilo Schwarz

Reputation: 782

That's an typical parse issue. You have to remember the last char you've read and deppendend on your current char, you know what to do.

    public static void main(String[] args) {
        String text = "Periods,hyphens, the last two characters cannot be a period.The rest of them don't. And there you go.";

        StringBuilder fixedString = new StringBuilder();
        String[] arrOfStr = text.split("");

        String lastChar = null;
        for(String currentChar : arrOfStr) {
            if(lastChar != null && (lastChar.equals(",") || lastChar.equals(".")) && !currentChar.equals(" "))
                fixedString.append(" ");

            fixedString.append(currentChar);
            lastChar = currentChar;
        }
        
        System.out.println(fixedString);
    }

Side note: The shortest and most elegant solution is, of course, @bohemian's. But I suspect it is not in the sense of the homework! ;-)

Upvotes: 0

Adriaan Koster
Adriaan Koster

Reputation: 16209

Your main problem is here:

Iterator i = stringArr.iterator();
// ...
for (String s : arrOfStr) {
    if (s.equals(".") && i.next().equals()) {

You are iterating both with an iterator and a for-loop, that makes life complicated. I'd suggest using a for-loop over the index.

Also your second equals expression is missing an argument.

Since Bohemian already posted the regex one liner, I might as well post my solution too:

public class PunctuationFixer {

    private String text;
    private int index;

    public static String addMissingWhitespaceAfterPunctuation(String input) {
        return new PunctuationFixer(input).fix();
    }

    private PunctuationFixer(String input) {
        this.text = input;
        this.index = 0;
    }

    private String fix() {
        while (index < this.text.length() - 1) {
            fix(this.text.charAt(index));
            index++;
        }
        return this.text;
    }

    private void fix(char current) {
        if (current == '.' || current == ',') {
            addSpaceIfNeeded();
        }
    }

    private void addSpaceIfNeeded() {
        if (' ' != (text.charAt(index + 1))) {
            text = text.substring(0, index + 1) + " " + text.substring(index + 1);
            index++;
        }
    }

    public static void main(String[] args) {
        String text = "Periods,hyphens, the last two characters cannot be a period. The rest of them don't. And there you go.";
        System.out.println(addMissingWhitespaceAfterPunctuation(text));
    }
}

Upvotes: 0

Bohemian
Bohemian

Reputation: 424993

You're over-thinking it:

String refactorSeparators(String str) {
    return str.replaceAll("([,.]) ?", "$1 ").trim();
}

The regex ([,.]) ? matches a comma or dot optionally followed by a space, which is replaced with the dot/comma and a space. The trim() removes the space that would be added if there's a dot at the end of the input.

Upvotes: 1

Related Questions