CodeGym
Promotion
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
MecoS
Level 16
  • 08.12.2019
  • 768views
  • 2comments

Maybe i didn't understand the question

Question about the task Software update
Java Syntax,  Level 8,  Lesson 11
Under discussion

Task: The program determines which family (last name) lives in the house with the specified number.
New task: The program should work with cities rather than house numbers:

Example input:
Chicago
Capone
New York City
Rockefeller
Seattle
Gates

Seattle

Example output:
Gates

Requirements:
  • The program should display text on the screen.
  • The program should read values from the keyboard.
  • The Solution class must have one method.
  • The program should display the family's last name based on the entered city.
package com.codegym.task.task08.task0829; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /* Software update */ public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); HashMap<String,String>familes = new HashMap<>(); familes.put("Chicago","Capone"); familes.put("New York City","Rockefeller"); familes.put("Seattle","Gates"); for (Map.Entry<String,String> cityes:familes.entrySet() ) { System.out.println(cityes.getKey() + "\n" + cityes.getValue()); } String family = reader.readLine(); for (Map.Entry<String,String> cityes:familes.entrySet() ) { if(cityes.getKey().equalsIgnoreCase(family)){ System.out.println(cityes.getValue()); } } // // List of addresses // List<String> addresses = new ArrayList<>(); // while (true) { // String family = reader.readLine(); // if (family.isEmpty()) break; // // addresses.add(family); // } // // // Read the house number // int houseNumber = Integer.parseInt(reader.readLine()); // // if (0 <= houseNumber && houseNumber < addresses.size()) { // String familyName = addresses.get(houseNumber); // System.out.println(familyName); // } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
8 December 2019, 16:46
This code has hard coded the example inputs in at lines 21-23. The task requires the user to enter those values instead. Get rid of those lines and code it so that the user can enter them. This is what the program should do: 1) Read in and stores pairs (City, Family) until the user enters an empty/blank string 2) takes in 1 more user input, a city 3) displays the family that is in the city from step #2
+1
MecoS Full Stack Developer at Microsoft
9 December 2019, 11:21
Thanks. Now i understand.
0
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.