It works on my machine though
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.*;
import java.util.ArrayList;
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();
//make an arraylist of arraylists to store all arguments from every line in a separate location
ArrayList<ArrayList<String>> fileData = fileBreakDown(fileName);
if (args.length > 0 && args[0].equals("-c")){
String[] arguments = new String[]{args[1],args[2],args[3]};
addToList(fileData, arguments);
}
//write all data back to the file
try {
FileWriter out = new FileWriter(fileName);
for (ArrayList<String> oneLine : fileData){
out.write(String.format("%-8s", oneLine.get(0)));
out.write(String.format("%-30s", oneLine.get(1)));
out.write(String.format("%-8s", oneLine.get(2)));
out.write(String.format("%-4s", oneLine.get(3)));
out.write("\r\n");
}
out.close();
} catch (IOException e) {}
}
private static ArrayList<ArrayList<String>> fileBreakDown(String fileName){
String st;
ArrayList<ArrayList<String>> fileData = new ArrayList<>();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(new File(fileName)));
//retrieve data from each stored line in the file and add them to the arraylist
while ((st = br.readLine()) != null){
ArrayList<String> lineData = new ArrayList<>();
lineData.add(st.substring(0, 8).trim()); //8 possible characters, no place-holders
lineData.add(st.substring(8, 38).trim()); //30 possible characters, no place-holders
lineData.add(st.substring(38,46).trim()); //8 possible characters, no place-holders
lineData.add(st.substring(46).trim()); //4 possible characters, no place-holders
fileData.add(lineData);
}
br.close();
} catch (IOException e) {}
return fileData;
}
private static void addToList(ArrayList<ArrayList<String>> fileData, String[] newEntryArray){
//find the proper id number for the new line of data
String lastStrId;
int lastIntID;
int nextID = 0;
for (int i = 0; i < fileData.size(); i++){
lastStrId = fileData.get(i).get(0);
lastIntID = Integer.parseInt(lastStrId.trim());
if (lastIntID > nextID){
nextID = lastIntID;
}
}
//add the new line derived from args to the arraylist made of all file data
ArrayList<String> newLine = new ArrayList<>();
newLine.add(Integer.toString(nextID+1));
newLine.add(newEntryArray[0]);
newLine.add(newEntryArray[1]);
newLine.add(newEntryArray[2]);
fileData.add(newLine);
}
}