Не записывает нужное количество пробелов после всех значений.
Соответственно, не находит самой большой индекс в последствии
package com.javarush.task.task18.task1827;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
File file = new File(fileName);
if(args[0].equals("-c")){
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
// ID
int id;
if(file.length() == 0) id = 1;
else {
int biggestId = 0;
while (fileReader.ready()){
String line = fileReader.readLine();
String indexWithSpaces = line.substring(0,8);
String indexWithoutSpaces = indexWithSpaces.trim();
int currentIndex = Integer.parseInt(indexWithoutSpaces);
if(currentIndex > biggestId) biggestId = currentIndex;
}
id = biggestId+1;
}
StringBuilder strId = new StringBuilder(Integer.toString(id));
if(strId.length() < 8){
for (int i = 0; i < 8-strId.length(); i++) {
strId = strId.append(" ");
}
}
// Product name
String productName = args[1];
if(productName.length() < 30){
for (int i = 0; i < 30-productName.length(); i++) {
productName = productName + " ";
}
}
// Price
String price = args[2];
if(price.length() < 8){
for (int i = 0; i < 8-price.length(); i++) {
price = price + " ";
}
}
// Quantity
String quantity = args[3];
if(quantity.length() < 4){
for (int i = 0; i < 4 - quantity.length(); i++) {
quantity = quantity + " ";
}
}
String product = strId + productName + price + quantity;
FileWriter writer = new FileWriter(fileName,true);
writer.write(product + "\n");
writer.close();
fileReader.close();
}
}
}