Advertisement
MilaDimitrovaa

Homework-Dictionary

Mar 19th, 2021
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.  
  10.         Dictionary<Integer, String> month = new Hashtable();
  11.  
  12.         //      key : value
  13.         month.put(1, "January");
  14.         month.put(2, "February");
  15.         month.put(3, "March");
  16.         month.put(4, "April");
  17.         month.put(5, "May");
  18.         month.put(6, "June");
  19.         month.put(7, "July");
  20.         month.put(8, "August");
  21.         month.put(9, "September");
  22.         month.put(10, "October");
  23.         month.put(11, "November");
  24.         month.put(12, "December");
  25.  
  26.         // принтираме размера на речника
  27.         System.out.println();
  28.         System.out.println("NUMBER OF ELEMENTS : " + month.size());
  29.         System.out.println();
  30.  
  31.         //  принтираме елементите на самият речник
  32.         System.out.println("DICTIONARY : " + month);
  33.         System.out.println();
  34.  
  35.  
  36.         System.out.print("--- PLEASE INPUT name OR number! --- : ");
  37.  
  38.         String choice = scan.nextLine();
  39.  
  40.  
  41.         if (choice.equalsIgnoreCase("number")) {
  42.             System.out.print("Input number: ");
  43.             int num = scan.nextInt();
  44.             System.out.printf("Value at key [%d] = %s", num, month.get(num));
  45.         }
  46.         if (choice.equalsIgnoreCase("name")) {
  47.             System.out.print("Input name: ");
  48.             String name = scan.nextLine();
  49.             int key = 0;
  50.             for (int i = 1; i <= 12; i++) {
  51.                 if (month.get(i).equalsIgnoreCase(name)) {
  52.                     key = i;
  53.                 }
  54.             }
  55.             System.out.printf("Key of value [%s] = %d", name, key);
  56.  
  57.         }
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement