Or am I missing something?
package com.codegym.task.task18.task1822;
/*
Finding data inside a file
The file data is separated by spaces and stored in the following order:
id productName price quantity
where id is an int
productName is a String — it can contain spaces
price is a double
quantity is an int
The information for each product is stored on a separate line.
*/
import java.io.*;
public class Solution {
//private static Map<Integer, String[]> products = new HashMap<>();
public static void main(String[] args) throws IOException {
//step1: read a filename from the console
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file = reader.readLine();
//step2: Create an inputStream for the file.
//I wrap the inputStream in a bufferedReader as I want t use readLine() method
BufferedReader reader2 = new BufferedReader(new FileReader(file));
String product;
while ((product = reader2.readLine()) != null) {
//in case there are spaces at the beginning of the line
//String[] productFeatures = product.trim().split(" ");
//if(args[0].equals(productFeatures[0])){
if(product.startsWith(args[0] + " ")) {
System.out.println(product);
break;
}
reader.close();
reader2.close();
}
}
}
/* //step3: read the content of the file
//each line has all information of 1 product
int id;
String productName = null;
double price;
int quantity;
String productLine;
while (!(productLine = reader2.readLine()).isEmpty()) {
//step4: get the product information separated from its id and add it to the map
*//* id = Integer.parseInt(product.substring(0, product.indexOf(" ")));
//String productDescription = product.substring(product.indexOf(" ") + 1);
products.put(id, product);*//*
//step4 seperate the productString by spaces and add them to the variables
String[] product = productLine.split(" ");
//FIXME: or do I have to split up all the individual product specifications?
for (int i = 0; i < product.length; i++) {
id = Integer.parseInt(product[0]);
//the before last string is the price
price = Double.parseDouble(product[product.length - 2]);
//the last string in the array is the quantity
quantity = Integer.parseInt(product[product.length - 1]);
//the productName can consist of several individual string words
if (product[i].contains("[a-zA-Z")) {
//concatenate all the words to 1 string called productName
productName += product[i];
}
products.put(id, product);
}
reader.close();
reader2.close();
}
}
//step 5: Search the file for information related to the specified id
// Display it in the format used in the file.
public static void printProduct ( int id){
String[] values = products.get(id);
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
}
}
*/