Совсем не соображу, что же я делаю не так. Очень хочется услышать мнения экспертов.
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
if (!(args.length == 0)) {
if (args[0].equals("-c")) {
FileWriter fileWriter = new FileWriter(fileName, true);
String nextId = maxInFile(fileName);
String id, productName, price, quantity;
if (nextId.length()>8){
id = nextId.substring(0,8);
}else id = nextId;
if(args[1].length()>30){
productName = args[1].substring(0,30);
}else productName=args[1];
if (args[2].length()>8){
price = args[2].substring(0,8);
}else price=args[2];
if (args[3].length()>4){
quantity = args[3].substring(0,4);
}else quantity = args[3];
String nextLine = String.format("%-8s%-30s%-8s%-4s", id, productName, price, quantity);
fileWriter.append("\n");
fileWriter.append(nextLine);
fileWriter.close();
}
}
}
public static String maxInFile(String fileName) throws Exception {
BufferedReader input = new BufferedReader(new FileReader(fileName));
String line;
int max = 0;
while (null != (line = input.readLine())) {
String[] stroke = line.split(" ");
if(stroke[0].length()>8){
stroke[0].substring(0,8);
}
if (max < Integer.parseInt(stroke[0])) {
max = Integer.parseInt(stroke[0]);
}
}
input.close();
max+=1;
return Integer.toString(max);
}
}