0

Enter a file name from the console. Find the byte or bytes with the maximum number of repetitions. Display them on the screen, separated by spaces. Close the IO stream.

How to solve this task?

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String fileName = reader.readLine();
    FileInputStream inputStream = new FileInputStream(fileName);
    Map<Integer, Integer> map = new HashMap<>();
    int data;
    Integer max = 0;
    while (inputStream.available() > 0) {
        data = inputStream.read();
        if (map.containsValue(data)) map.replace(data, map.get(data) + 1);
        else map.put(data, 1);
        if (map.get(data) > max) max = map.get(data);
    }
        inputStream.close();
    for (Map.Entry<Integer, Integer> entry : map.entrySet()){
        if(entry.getValue() > max) {
            System.out.print(entry.getKey() + "/t");
        }
    }

    }
}
Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
  • what is your code actually doing? what do you expect it to do? what are the errors you are getting? – aran Jul 24 '19 at 09:08

3 Answers3

0

That line:

if (map.containsValue(data)) ... 

That is not what you want. You read a byte from the file, and you want to know if that byte is already stored. As key, not as value!

Thus you need the corresponding logic, like this:

int counterForData = 0;
if (map.contains(data)) {
  counterForData = map.get(data);
} 
map.put(data, counterForData+1);

Meaning: your keys are the "data bytes", and your values are the corresponding frequency.

And when that code works, you can start into looking into writing even less code, for example by using map.getOrDefault(), or even better computeIfAbsent()!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

This line...

if (map.containsValue(data)) map.replace(data, map.get(data) + 1);
else map.put(data, 1);

checks if map already contains value (which it wont because your map is empty). You want to increase the count of the data, if it exists or add the data if it doesnt exist already. To do this replace above snippet with this...

map.compute(data, (k, v) -> (v == null) ? 1 : v + 1);

This will add new data as [data, 1] and update existing data as [data, previous value + 1]. If you use this, you should read about compute function

Ashvin Sharma
  • 563
  • 1
  • 5
  • 24
0
  • containsKey would be the correct method i.o. containsValue.
  • available gives how many bytes can be read from buffered input, without blocking for waiting on physical reading. This means that when the low-level I/O buffer is empty, there still could be data. The solution is to let the code "block", wait on next physical reading.
  • The result: all bytes with a maximal count requires a walk of >= or == max.

Hence:

Map<Byte, Integer> map = new HashMap<>();
int maxCount = 0;
//byte maxByte = 0;
int data;
while ((data = inputStream.read()) >= 0) {
    byte b = (byte)data;
    int count = map.getOrDefault(b, 0) + 1;
    map.put(b, count);
    if (count > maxCount) {
        maxCount = count;
        //maxByte = b;
    }
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()){
    if (entry.getValue() == maxCount) {
        System.out.print(entry.getKey() + "\t");
    }
}
System.out.println();
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138