CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
ilian44
Level 30
Veliko Turnovo
  • 28.11.2019
  • 877views
  • 2comments

output all keys who's value's are equal to min and still the task failed??

Question about the task Rarest bytes
Java Core,  Level 8,  Lesson 3
Resolved

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.

Requirements:
  • The program should read a file name from the console.
  • Use a FileInputStream to read from the file.
  • All of the least frequently repeated bytes from the file should be displayed, separated by spaces.
  • The screen output should be displayed in one line.
  • The stream used to read the file must be closed.
package com.codegym.task.task18.task1804; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.Collections; import java.util.Map; import java.util.TreeMap; /* Rarest bytes */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String sAge = reader.readLine(); // String sAge="/home/r00t/test/file3"; FileInputStream inputStream = new FileInputStream(sAge); reader.close(); TreeMap<Integer,Integer> map=new TreeMap<Integer,Integer>(); boolean check = true; Integer compare = null; while (inputStream.available() > 0) //as long as there are unread bytes { int data = inputStream.read(); //Read the next byte if (map.containsKey(data)) { int value = map.get(data); map.put(data,++value); } else { map.put(data, 1); } } inputStream.close(); for(Map.Entry m:map.entrySet()){ if (check) { compare = Integer.parseInt(m.getValue().toString()); check = false; } if (compare==Integer.parseInt(m.getValue().toString())) { // Integer condition = Integer.parseInt(m.getKey().toString()); // Integer condition2 = 20; // if (condition > condition2) { System.out.print(m.getKey() + " "); // } } } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
ilian44
Level 30 , Veliko Turnovo, Bulgaria
29 November 2019, 07:19
Thanks, dude! I managed to solve it!
0
Guadalupe Gagnon
Level 37 , Tampa, United States
29 November 2019, 01:45
Tree maps sort by key, so when you set compare at line 54 you are setting it to the value of the 'lowest' key. This key has a very low chance of actually being the least repeated key in the file. To solve this task you need to find the lowest value and output the key (or keys) that match that value.
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.