Why isn't this correct? And any idea how to test it?
What is byte in a text file?
package com.codegym.task.task18.task1804;
import java.io.FileInputStream;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
Rarest bytes
Enter a file name from the console.
Find the byte or bytes with the minimum number of repetitions.
Display them on the screen, separated by spaces.
Close the IO stream.
*/
public class Solution {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String fileName = sc.nextLine(); // Read user input
HashMap<Integer, Integer> map = new HashMap<>();
FileInputStream inputStream = new FileInputStream(fileName);
int min = Integer.MAX_VALUE;
while (inputStream.available() > 0) { //as long as there are unread bytes
int data = inputStream.read(); //Read the next byte
if (!map.containsKey(data)) {
map.put(data, 1);
}
{
map.replace(data, map.get(data) + 1);
}
if (map.get(data) <= min) {
min = map.get(data);
}
}
inputStream.close(); // Close the stream
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() == min) {
System.out.print(entry.getKey() + " ");
}
}
sc.close();
}
}