Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap') - I don't understand why I get this error. I cannot check the output to see what I am doing
package com.codegym.task.task18.task1804;
import java.io.*;
import java.lang.annotation.Target;
import java.util.*;
/*
Rarest bytes
1. The program should read a file name from the console.
2. Use a FileInputStream to read from the file.
3. All of the least frequently repeated bytes from the file should be displayed, separated by spaces.
4. The screen output should be displayed in one line.
C:\Users\40742\Desktop\BIOCHIMIE2.txt*/
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<Integer> arrays = new ArrayList<>();
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String fileName = buffer.readLine();
FileInputStream input = new FileInputStream(fileName);
while(input.available() > 0){
int read = input.read();
arrays.add(read);
}
input.close();
leastRepeated(arrays);
}
public static void leastRepeated(ArrayList array){
HashMap<Integer,Integer> hash = new HashMap<>();
ArrayList<Integer> ar = new ArrayList<>(hash.values());
for(Object x : array){
hash.put(Integer.parseInt((String) x),Collections.frequency(array,x));
}
Collections.sort(ar);
int i = 0;
for(Map.Entry<Integer,Integer> x : hash.entrySet()){
if(x.getValue().equals(ar.get(i))){
System.out.print(x.getKey() + " " + ar.get(i));
i++;
}
}
}
}