То работает, то не работает, такое чувство, что программа своей жизнью живет) Смотрел через дебаггер, как я понимаю, главная дичь в том, что в тех местах кода, где надо сравнить id с восьмизначной частью строки, сравнения тупо не происходит. А если сравнивать по значению int, то при попытке спарсить Integer.parseInt(), выбрасывается исключение NumberFormatException. то есть невозможно конвертировать строку "19847" в чисто 19847. Объясните пож, где косяк))
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Good {
public String id;
public String productName;
public String price;
public int quantity;
public Good(String id, String productName, String price, int quantity) {
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
void refresher (String fileName) throws IOException, FileNotFoundException {
List<String> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while (reader.ready()) {
String line = reader.readLine();
list.add(line);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for (String x : list) {
if (x.substring(0, 8).trim().equals(String.valueOf(this.id))) {
writer.write(builder(this) + System.lineSeparator());
continue;
}
writer.write(x + System.lineSeparator());
}
writer.close();
}
String builder (Good good) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(this.id);
while (stringBuilder.length() != 8) stringBuilder.append(" ");
if (this.productName.length() <= 30) {
stringBuilder.append(this.productName);
while (stringBuilder.length() != 38) stringBuilder.append(" ");
}
else {
for (int i = 0; i < 30; i++) stringBuilder.append(productName.toCharArray()[i]);
}
stringBuilder.append(this.price);
while (stringBuilder.length() != 46) stringBuilder.append(" ");
stringBuilder.append(this.quantity);
while (stringBuilder.length() != 50) stringBuilder.append(" ");
return String.valueOf(stringBuilder);
}
}