I have tried running this in IntellijIDEA and I can't quite figure out where the issue is.
The last validation check says that my input stream isn't closed, but I explicitly close it before opening the output stream.
Can someone help point me in the right direction?
package com.codegym.task.task18.task1827;
import java.io.*;
import java.util.*;
/*
Prices
*/
public class Solution {
public static void main(String[] args) throws Exception {
if(args[0].equals("-c")){
ArrayList<String> lines = new ArrayList<String>();
System.out.println(args[0] + args[1] + args[2] + args[3]);
BufferedReader fileNameReader = new BufferedReader(new InputStreamReader(System.in));
String filename = fileNameReader.readLine();
int maxId = 0;
//create file reader for File fileName
Scanner input = new Scanner(new File(filename));
while(input.hasNextLine()){
//read in all the data and store it.
String line = input.nextLine();
String id = line.substring(0,8).replace(" ", "");
if(Integer.parseInt(id) > maxId){
maxId = Integer.parseInt(id);
}
lines.add(line);
}
fileNameReader.close();
input.close();
//int currentMaxId = Integer.parseInt(ids.get(ids.size()-1));
int newId = maxId + 1;
String idString = newId + "";
if(idString.length() > 8){
idString = idString.substring(0,8);
}
while(idString.length() < 8){
idString += " ";
}
String newProductName = args[1];
if(newProductName.length() > 30){
newProductName = newProductName.substring(0,30);
}
while(newProductName.length() < 30){
newProductName += " ";
}
String newPrice = args[2];
if(newPrice.length()>8){
newPrice = newPrice.substring(0,8);
}
while(newPrice.length() < 8){
newPrice += " ";
}
String newQuantity = args[3];
if(newQuantity.length() > 4){
newQuantity = newQuantity.substring(0,4);
}
while(newQuantity.length() < 4){
newQuantity += " ";
}
PrintWriter writer = new PrintWriter(filename);
for(int i = 0; i < lines.size();i++){
writer.println(lines.get(i));
}
writer.println(newId + newProductName + newPrice + newQuantity);
writer.close();
}
else{
return;
}
}
}