I also tried running this using a file in my local drive .txt file, saved in the below format,
id 324
productName laptop
price 45.23
quantity 4
id 323
productName desktop cable
price 43.22
quantity 12
i get the correct output if i search for 323 it gives me below output:
id 323
productName desktop cable
price 43.22
quantity 12
However i m not passing the codegym validation. What should I change in my code?
package com.codegym.task.task18.task1822;
/*
Finding data inside a file
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
int searchId = Integer.valueOf(args[0]);
ArrayList<String> fileDataList = new ArrayList<>();
// BufferedReader bufferedFileReader = new BufferedReader(new FileReader("C:/Users/Dan/Desktop/idFile.txt"));
try (BufferedReader bufferedFileReader = new BufferedReader(new FileReader(fileName))) {
while(bufferedFileReader.ready()) {
String[] splitted = bufferedFileReader.readLine().split(" ");
for (String split : splitted) {
fileDataList.add(split);
}
}
}
for (int i = 0; i < fileDataList.size(); i++) {
if(fileDataList.get(i).equals(String.valueOf(searchId))) {
System.out.println(fileDataList.get(i - 1) + " " + Integer.parseInt(fileDataList.get(i)));
System.out.print(fileDataList.get(i + 1) + " " + fileDataList.get(i + 2));
while(!fileDataList.get(i + 3).equals("price")) {
System.out.println(" " + fileDataList.get(i + 3));
i++;
}
System.out.println(fileDataList.get(i + 3) + " " + Double.parseDouble(fileDataList.get(i + 4)));
System.out.println(fileDataList.get(i + 5) + " " + Integer.parseInt(fileDataList.get(i + 6)));
i = fileDataList.size();
break;
}
}
reader.close();
// System.out.println(fileDataList.toString());
}
}