Word Processor 2020 USACO January Bronze first problem

Please look at my code for Word Processor in the 2020 Bronze USACO contest in January. The only that’s wrong in testing is that there shouldn’t be spaces at the end of each line in the final output. I can’t fix this. Please read the problem is needed in the USACO website. Here’s my code:

import java.io.;
import java.util.
;

public class program {
public static void main(String[] args) throws IOException{
BufferedReader r = new BufferedReader(new FileReader(“word.in”));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(“word.out”)));
StringTokenizer st = new StringTokenizer(r.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int Kreplica = K;
String[] string = new String[N];
st = new StringTokenizer(r.readLine());
for (int i = 0; i < N; i++)
string[i] = st.nextToken();
for (int j = 0; j < N; j++){
if (string[j].length() < K){
pw.print(string[j] + " “);
K = K - string[j].length();}
else if (string[j].length() == K){
pw.println(string[j]);
K = Kreplica;}
else if (string[j].length() > K){
pw.println(”");
pw.println(string[j]);
K = Kreplica;}}

r.close();
pw.close();
}
}

Not sure what the question is. It is indeed true that your code will be marked incorrect if you output extra spaces.