import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
FileInputStream fis = new FileInputStream(fileName);
byte[]buffer = new byte[fis.available()];
while(fis.available()>0){
fis.read(buffer);
String s = new String(buffer);
String[] stringArr = s.split(" ");
if(stringArr[0].equals(args[0])){
System.out.print(s);
break;
}
}
fis.close();
}
}
I managed to solve this problem with BufferedReader, but im interested in what is wrong with this solution if anybody can tell me please!
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
1 November 2019, 13:34solution
It looks like this code reads everything in at once and then split by " ", which makes the while loop pointless. After everything was read in it was converted to a string then split by " ". It searched for the ID in only one location after that, stringArr[0]. For example, lets say the data is this:
1 paint 15.67 13
2 suncreen 5.67 22
31245678 umbrella 30.99 5
3124567 swim trunks delight 14.87 22
and you wanted to find id "31245678", which is the third line down. Because this code never splits by line, it would only check the very first element of the split by " ", which would be "1".
What should have done is split by each new line first then for each line split by " " and compared the first element of each line for id. The BufferedReader.readLine() method only reads one line at a time, so if you had replaced what is above with a buffered reader instead the code would have been checking each new line for ID instead and passed verification.
+2
Picvajzmen
5 November 2019, 07:26
Thank you Guadalupe, did the whole thing again but this time i was understanding the problem at hand. I was not getting the problem with reading the whole file and reading just one line before, i was ignoring it.
+1