What's wrong?
Both of my solutions seem to work correctly when I test them manually.
package 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.HashMap;
public class Solution {
public static void main(String[] args) {
/* String fileName = "";
char[] contents;
String sContents = "";
HashMap<Integer, String> map = new HashMap<>();
int numID = Integer.parseInt(args[0]);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)))
{
fileName = reader.readLine();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
try (FileInputStream fis = new FileInputStream(fileName))
{
contents = new char[fis.available()];
for (int i = 0; i < contents.length; i++) {
contents[i] = (char) fis.read();
}
sContents = String.valueOf(contents);
String[] sArray = sContents.split("\r\n");
for (String entry: sArray
) {
String sIndex = entry.split(" ",2)[0];
int index = Integer.parseInt(sIndex);
String item = entry.split(" ",2)[1];
map.put(index, item);
}
if (map.containsKey(numID))
{
System.out.print(numID + " ");
System.out.println(map.get(numID));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}*/
String fileName = "";
String id = args[0];
byte[] buffer;
String contents = "";
String[] lines;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)))
{
fileName = reader.readLine();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
try (FileInputStream fis = new FileInputStream(fileName))
{
buffer = new byte[fis.available()];
fis.read(buffer);
contents = new String(buffer);
lines = contents.split("\r\n");
for (String line: lines
) {
if (line.split(" ")[0].equals(id))
{
System.out.print(line);break;
}
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}