I just have no clue what they want with the third requirement
Edit: I tried changing line 40 to j=0, just in case the ID should be included in the displayed info; still failing the same condition
Edit2: I changed lines 33-47 to:
HashMap<Integer, String> idAndInfo = new HashMap<>();
ArrayList<Integer> ids = new ArrayList<>();
//add all products to HashMap with Key = ID and Value = product info
for (int i = 0; i<products.size(); i++){
String[] info = products.get(i).split(" ");
int id = Integer.parseInt(info[0]);
ids.add(id);
String productInfo = "";
for (int j = 1; j<info.length-1;j++){
s += (info[j] + " ");
}
s+= info[info.length-1];
idAndInfo.put(id, productInfo);
}
for (int i = 0; i<ids.size(); i++)
productSearch(idAndInfo, ids.get(i));
(again with and without the ID in the displayed info) - still not passingpackage com.codegym.task.task18.task1822;
/*
Finding data inside a file
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class Solution {
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
FileInputStream fileInputStream = new FileInputStream(fileName);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(fileInputStream));
//read all lines from file and add to ArrayList
String s;
ArrayList<String> products = new ArrayList<>();
while ((s=fileReader.readLine())!=null){
products.add(s);
}
reader.close();
fileInputStream.close();
fileReader.close();
HashMap<Integer, String> idAndInfo = new HashMap<>();
//add all products to HashMap with Key = ID and Value = product info
for (int i = 0; i<products.size(); i++){
String[] info = products.get(i).split(" ");
int id = Integer.parseInt(info[0]);
String productInfo = "";
for (int j = 1; j<info.length-1;j++){
s += (info[j] + " ");
}
s+= info[info.length-1];
idAndInfo.put(id, productInfo);
}
productSearch(idAndInfo, 10);
}
//method for searching product info based on passed ID argument
public static void productSearch(HashMap<Integer, String> hashMap, Integer id){
for (HashMap.Entry<Integer, String> pair : hashMap.entrySet()){
if (id.equals(pair.getKey()))
System.out.println(pair.getValue());
}
}
}