Вроде все правильно работает, но валидатор не пропускает.
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) throws Exception {
// args = new String[]{"-c", "майка белая", "12.00", "5"};
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String path = reader.readLine();
FileWriter fileWriter = new FileWriter(path, true);
reader = new BufferedReader(new FileReader(path));
if (args[0] == "-c") {
String stringID = "";
String productName = args[1];
String price = args[2];
String quantity = args[3];
ArrayList<Long> listOFId = new ArrayList<>(); //ищем максимальный ID и инкрементируем
long longID;
while (reader.ready()) {
String str = reader.readLine();
for (int i = 0; i < 8; i++) {
if (str.substring(i, i + 1).equals(" ")) {
break;
}
else {
stringID += str.substring(i, i + 1);
}
}
longID = Long.parseLong(stringID);
listOFId.add(longID);
stringID = "";
}
String newID = String.valueOf(Collections.max(listOFId) + 1);
char[] arrForID = newID.toCharArray();
newID = "";
for (int i = 0; i < 8; i++) {
if (i >= arrForID.length) {
newID += " ";
}
else newID += arrForID[i];
}
// создаем строку с наименованием продукта
if (args[1].length() < 30) {
for (int i = args[1].length(); i < 30; i++) productName += " ";
}
else if (args[1].length() > 30) productName = args[1].substring(0, 30);
// создаем строку с ценой
if (args[2].length() < 8) {
for (int i = args[2].length(); i < 8; i++) price += " ";
}
else if (args[2].length() > 8) price = args[2].substring(0, 8);
// создаем строку с количеством
if (args[3].length() < 4) {
for (int i = args[3].length(); i < 4; i++) quantity += " ";
}
else if (args[3].length() > 4) quantity = args[3].substring(0, 4);
fileWriter.write(newID + productName + price + quantity); //добавляем новую строку в файл
newID = "";
productName = "";
price = "";
quantity = "";
reader.close();
fileWriter.close();
}
else if (args[0] != "-c") {
fileWriter.close();
reader.close();
}
}
}