все работает, не проходит валидацию: требования не выполнены по пункту "-u"
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
if (args.length != 0) {
try {
FileWriter fw = new FileWriter(fileName);
BufferedReader br = new BufferedReader(new FileReader(fileName));
if (args[0].equals("-c")) {
String line, result;
int id = 0;
while ((line = br.readLine()) != null) {
if (Integer.parseInt(line.substring(0, 8).trim()) > id) {
id = Integer.parseInt(line.substring(0, 8).trim());
}
}
id++;
result = "\n" + String.format("%-8s%-30s%-8s%-4s",id,args[1],Double.parseDouble(args[2]),Integer.parseInt(args[3]));
fw.write(result);
fw.flush();
}
if (args[0].equals("-d")) {
String line;
ArrayList<String> result = new ArrayList<>();
int id = Integer.parseInt(args[1]);
while ((line = br.readLine()) != null) {
if (Integer.parseInt(line.substring(0, 8).trim()) != id) {
result.add(line);
}
}
for (String s: result) {
fw.write(s);
fw.write("\n");
}
fw.flush();
}
if (args[0].equals("-u")) {
String line;
ArrayList<String> result = new ArrayList<>();
int id = Integer.parseInt(args[1]);
while ((line = br.readLine()) != null) {
if (Integer.parseInt(line.substring(0, 8).trim()) == id) {
result.add(String.format("%-8s%-30s%-8s%-4s",Integer.parseInt(args[1]),args[2],Double.parseDouble(args[3]),Integer.parseInt(args[4])));
} else result.add(line);
}
for (String s: result) {
fw.write(s);
fw.write("\n");
}
fw.flush();
}
fw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
scanner.close();
}
}