CodeGym
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Gellert Varga
Level 23
Szekesfehervar
  • 10.10.2021
  • 186views
  • 1comment

Should they be sorted alphabetically or in ascending order of salaries??

Question about the task Calculating salaries
Java Core,  Level 9,  Lesson 11
Resolved

The first parameter of the main method is a file name.
Each line of the specified file has the following format:
name value
where [name] is a String and [value] is a double. [name] and [value] are separated by a space.

Find the sum of all the values for each name.
Display all the data, sorted by name in ascending order.
Close the streams.

Example input file:
Peterson 2
Smith 6
Baxter 1.35
Peterson 3.1

Example output:
Baxter 1.35
Peterson 5.1
Smith 6.0

Requirements:
  • The program must NOT read data from the console.
  • The program must read the file's contents (use FileReader).
  • The file input stream (FileReader) must be closed.
  • The program should write to the console each name, sorted in ascending order, and the sum of all its values.
package com.codegym.task.task19.task1919; /* Calculating salaries */ import java.util.*; import java.io.*; // T1919 public class Solution { public static void main(String[] args) throws Exception { // args = new String[] { "/uploads/teszt.txt", "blabla" }; // System.out.println(args[0] + ", " + args[1]); ArrayList<String> fileLines = new ArrayList<>(); BufferedReader bfr = new BufferedReader(new FileReader(args[0])); String s; while ((s = bfr.readLine()) != null) { if (s.isEmpty()) break; fileLines.add(s); } bfr.close(); HashMap<String, Double> hshm = new HashMap<>(); String key = null; Double d = null; for (int i=0; i<fileLines.size(); i++) { key = fileLines.get(i).substring(0, fileLines.get(i).indexOf(" ")); // System.out.print(key); d = Double.parseDouble(fileLines.get(i).substring(fileLines.get(i).indexOf(" ")+1)); // System.out.println(d); if (!hshm.containsKey(key)) { hshm.put(key, d); } else hshm.put(key, hshm.get(key)+d); } // System.out.println(hshm); HashMap<Double,String> hshmInverted = new HashMap<>(); for (Map.Entry<String, Double> pair : hshm.entrySet()) { hshmInverted.put(pair.getValue(), pair.getKey()); } // System.out.println(hshmInverted); ArrayList<Double> dbList = new ArrayList<>(hshmInverted.keySet()); Collections.sort(dbList); for(int i=0; i<dbList.size(); i++) { System.out.print(hshmInverted.get(dbList.get(i))+" "); System.out.println(dbList.get(i)); } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Lisa
Level 41
10 October 2021, 15:18useful
That's what they write: Display all the data, sorted by name in ascending order. I've implemented it that way and it validated. Bye bye dear colleague, yeah, yeah 😜🤪 Ahhh.. I didn't want to do it by I can not resist... what if the two persons have the same summed up values, young padawan? I must be crazy, ahhh, really crazy, crazy... why do I do that... 🤪😜
+5
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.