Got WA for all test cases even though it seems to give valid output on the ide?

``java
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner inputReader = new Scanner(System.in);
String line = inputReader.nextLine();

    String[] unconvertedValues = line.split(" ");

    int numWords = Integer.parseInt(unconvertedValues[0]);
    int maxChar = Integer.parseInt(unconvertedValues[1]);

    line = inputReader.nextLine();
    String[] words = line.split(" ");

    String linePrint = "";
    int currentChar = 0;


    for (int i = 0; i < numWords; i++) {
        if ((currentChar + words[i].length()) <= maxChar) {
            currentChar += words[i].length();
            linePrint += words[i] + " ";
        }else{
            System.out.println(linePrint);
            currentChar = 0;
            linePrint = "";
            i--;
        }
    }

    System.out.println(linePrint);
}

}


I submitted my code here [Link to CPI bronze class](https://usaco.guide/groups/d7eYGfddXq3m2trXG2xt/post/ZkhTvlw0KgyHrqQ69LD1/problems/iHaZTUqAEQqz7LzrAojM)

INPUT:
10 7
hello my name is Bessie and this is my essay

OUTPUT: 
hello my 
name is
Bessie
and this
is my
essay
import java.util.*;

public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        Scanner inputReader = new Scanner(System.in);
        String line = inputReader.nextLine();

        String[] unconvertedValues = line.split(" ");

        int numWords = Integer.parseInt(unconvertedValues[0]);
        int maxChar = Integer.parseInt(unconvertedValues[1]);

        line = inputReader.nextLine();
        String[] words = line.split(" ");

        String linePrint = "";
        int currentChar = 0;


        for (int i = 0; i < numWords; i++) {
            if ((currentChar + words[i].length()) <= maxChar) {
                currentChar += words[i].length();
                sb.append(words[i] + " ");
            }else{
                sb.deleteCharAt(sb.length() - 1);
                System.out.println(sb.toString());
                currentChar = 0;
                sb.setLength(0);
                i--;
            }
        }

        sb.deleteCharAt(sb.length() - 1);
        System.out.println(sb.toString());

        inputReader.close();
    }
}
```java

Thanks, I have passed all the cases now. It was the space at the end of each line I believe.