After about 2 days, I really just want to move on. What's the problem here?
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
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();
BufferedReader reader1 = new BufferedReader(new FileReader(fileName));
ArrayList<Integer> list = new ArrayList<>();
while(true) {
String line = reader1.readLine();
if(line == null) break;
int id = Integer.parseInt(line.substring(0, 8).replaceAll(" ", ""));
list.add(id);
}
reader1.close();
Collections.sort(list); // sorts in ascending order
int highestId = list.get(list.size() - 1); // assigns the highest id # to the highestId variable
if(args.length != 0) {
String id = String.valueOf(highestId + 1); // increments id by one for the next product to be added
String productName = args[1];
String price = String.valueOf(Double.parseDouble(args[2]) + "0");
String quantity = String.valueOf(Double.parseDouble(args[3]));
while(id.length() < 8) { //ensures id will get to length 8 as per requirements
id += " ";
}
while(productName.length() < 30) { //ensures product name will get to length 30 as per requirements
productName += " ";
}
while(price.length() < 8) { //ensures price will get to length 8 as per requirements
price += " ";
}
while(quantity.length() < 4) { //ensures quantity will get to length 4 as per requirements
quantity += " ";
}
byte[] bytes = (id + productName + price + quantity).getBytes();
FileOutputStream outputStream = new FileOutputStream(fileName, true); // Make sure contents are not overwritten.
outputStream.write(10); // 10 is new line in ASCII
outputStream.write(bytes);
outputStream.close();
}
}
}