Question

In: Computer Science

Okay, I have a general idea of how to use the scanner, but i'm a bit...

Okay, I have a general idea of how to use the scanner, but i'm a bit confused of how to ask multiple questions and also how maps work, which is making me struggle on this questions... If i had more time, i'd study a bit more, but I only have about 3 hours. If possible, could you explain every step with comments?

Use the scanner class to ask users their name and age. Store at least 10 users on a map, and use the iterator to print all the values. Print the youngest user's name

Solutions

Expert Solution

/*******************************YoungestUser.java***********************/

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class YoungestUser {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);// initialize the scanner

       Map<Integer, String> users = new HashMap<>();// initialize map

       /*
       * for loop to enter number of times you want to enter user
       */
       for (int i = 0; i < 3; i++) {

           System.out.print("Enter the name of user: ");// prompt for name
           String name = scan.nextLine();// read name as full line
           System.out.print("Enter the age of user: ");// prompt for age
           int age = scan.nextInt();// read age
           scan.nextLine();// clear the buffer after read nextInt
           users.put(age, name);// put data in map
       }

       Map<Integer, String> treeMap = new TreeMap<Integer, String>(users);//sort the map using age as key
       System.out.println("****************Print the users youngest to oldest********");
       Set<Entry<Integer, String>> entrySet = treeMap.entrySet();
       Iterator<Entry<Integer, String>> it = entrySet.iterator(); // using iterator iterate the entry set
       while (it.hasNext()) {
           Map.Entry<Integer,String> me = (Map.Entry<Integer,String>) it.next();// read value one by one
           System.out.println("Name is: " + me.getValue() + " and age is: " + me.getKey());// print the data
       }
      
       Entry<Integer, String> entry = treeMap.entrySet().iterator().next();
       System.out.println("The youngest users is: "+entry.getValue());
       scan.close();// close the scanner
   }
}
/****************output*****************/

Enter the name of user: JKS
Enter the age of user: 10
Enter the name of user: Virat
Enter the age of user: 56
Enter the name of user: Gagan
Enter the age of user: 6
****************Print the users youngest to oldest********
Name is: Gagan and age is: 6
Name is: JKS and age is: 10
Name is: Virat and age is: 56
The youngest users is: Gagan

Please let me know if you have any doubt or modify the answer, Thanks:)


Related Solutions

Hello, I'm having a hard time solving these problems because I have no idea on what...
Hello, I'm having a hard time solving these problems because I have no idea on what to do with accrued interest. Help is highly appreciated! On March 1, 2015, Bowan Corporation issued 6% bonds dated February 1, 2015, the face amount of $700,000. The bonds were sold for the present value of the bonds on March 1, 2015 plus one-month accrued interest. The bonds mature on January 31, 2018. Interest is paid semiannually on July 31 and January 31. Bowan's...
Is moderate use of alcohol okay? Is it ever okay to get drunk? If so, how...
Is moderate use of alcohol okay? Is it ever okay to get drunk? If so, how often? What about binge drinking—is it a dangerous prac- tice? Harmless fun? Is it up to the individual? Do you think alcoholism is a disease? 1 page paper please
Have a pretty good idea on how to solve this program, I'm just not very accustomed...
Have a pretty good idea on how to solve this program, I'm just not very accustomed to formatting well in Java for these types of problems. In java, you have full control of what's printed, I just need to get better at it, so I thought I'd post it here to receive an answer to learn from. Thanks for everything guys. This program must use the Exclusive OR Operator ^ in java, it must also use a while loop. The...
how to prepare a proposal for your business idea if i have a idea to open...
how to prepare a proposal for your business idea if i have a idea to open a restaurant . what will be the sample of business proposal like first 1.title page then 2.table of content whats next other stages
Okay, I have a case study I need to complete by the end of this week...
Okay, I have a case study I need to complete by the end of this week but I am stuck. Plus, I need to make sure that I am doing this correctly. It is a layered challenge. I will have to send the study in pieces because I could not attach the actual document.    Requirement #1:                                               During its first month of operation, the Quick Tax Corporation, which specializes in...
Black-Scholes This problem is a bit of work but I'm curious to see if I got...
Black-Scholes This problem is a bit of work but I'm curious to see if I got it right. I think i did XYZ stock is being traded at $115 today. Consider European put and call options with an exercise price of $110. The option expires in 91 days and the volatility is 0.50. The risk-free rate (continuously compounded) is 2.5%. Use actual/365 day count method. A) Calculate the values of the put and call options. Assume there will be no...
I have two Biophysical chem questions that I have no idea how to get the answer....
I have two Biophysical chem questions that I have no idea how to get the answer. Please help and show work. Thank you. 1. The unit millimeters of mercury (mmHg) has been replaced by the unit torr: 1mmHg is defined as the pressure at the base of a column of mercury exactly 1 mm high when its density is 13.5951 g/cm3 and the acceleration of free fall is 9.806 m/s2. What is the relation between the two units? The answer...
I have a lab assignment that I'm not sure how to do. The experiment is a...
I have a lab assignment that I'm not sure how to do. The experiment is a cart moving 60cm distance and there is a fan on top of it making it have a mass of .56kg. Every trial there is 100g added to the cart. For this part, the time is kept the same. 1. If the force provided by the fan was the same for each run and we have chosen the same time interval, how does the impulse...
REQUIREMENT #3 Okay, I have a case study I need to complete by the end of...
REQUIREMENT #3 Okay, I have a case study I need to complete by the end of this week but I am stuck. Plus, I need to make sure that I am doing this correctly. It is a layered challenge. I will have to send the study in pieces because I could not attach the actual document.    Prepare a trial balance for July.     During its first month of operation, the Quick Tax Corporation, which specializes in tax preparation,              ...
REQUIREMENT #8 Okay, I have a case study I need to complete by the end of...
REQUIREMENT #8 Okay, I have a case study I need to complete by the end of this week but I am stuck. Plus, I need to make sure that I am doing this correctly. It is a layered challenge. I will have to send the study in pieces because I could not attach the actual document.    Requirement #8:                                              Prepare the closing entries at July 31 in the General Journal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT