Hello everyone,
I think the code is working properly (at least I think my testing went fine according to the specs in condition.tcs, however the task is to being validated and I get the following error : The file streams must be closed. (Recommendation from your mentor : the file output stream was not closed), while I close it at line 80.
Does anyone understand what I'm doing wrong here ?
Thanks in advance,
Earil
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
//Reading file name and opening input and output streams
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
Scanner scanner = new Scanner(new FileInputStream(fileName));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fileName, true));
reader.close();
if(args[0].equals("-c")){
//find the maximum ID in the file
int maxID = 0;
while(scanner.hasNext()){
String line = scanner.nextLine();
String id = line.substring(0, 8);
id = id.substring(0, id.indexOf(" "));
int intID = Integer.parseInt(id);
if(intID > maxID){
maxID = intID;
}
}
//increment it to get the ID of the new product
maxID++;
//turn it into a string and add spaces to get to 8 characters
String newID = maxID + "";
while(newID.length() < 8){
newID += " ";
}
//Make the other arguments the correct number of characters
String productName = args[1];
String price = args[2];
String quantity = args[3];
while(productName.length() < 30){
productName += " ";
}
while (price.length() < 8){
price += " ";
}
while (quantity.length() < 4) {
quantity += " ";
}
//Append the new line to the file
outputStream.write(System.lineSeparator().getBytes());
outputStream.write(newID.getBytes());
outputStream.write(productName.getBytes());
outputStream.write(price.getBytes());
outputStream.write(quantity.getBytes());
}
//Close the streams
scanner.close();
outputStream.close();
}
}