Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6. public class HashMapSnow {
  7.  
  8. static Scanner stringScanner = new Scanner(System.in);
  9. static Scanner numberScanner = new Scanner(System.in);
  10.  
  11.  
  12. public static void main(String[] args) {
  13.  
  14. // Completed Array of snowfall amounts and month keys
  15. HashMap<String, Integer> snowfall = new HashMap();
  16. snowfall.put("January", 3);
  17. snowfall.put("February", 10);
  18. // snowfall.put("March", 3);
  19. // snowfall.put("April", 10);
  20. // snowfall.put("May", 3);
  21. // snowfall.put("June", 10);
  22. // snowfall.put("July", 3);
  23. // snowfall.put("August", 10);
  24. // snowfall.put("September", 3);
  25. // snowfall.put("October", 10);
  26. // snowfall.put("November", 3);
  27. // snowfall.put("December", 10);
  28.  
  29.  
  30. // todo add a loop to ask user for more data until they are done.
  31. // todo add code to check if month is already in HashMap, ask user if they want to overwrite or not?
  32.  
  33. System.out.println("Enter name of month");
  34. String userMonth = stringScanner.nextLine();
  35.  
  36. System.out.println("You entered " + userMonth);
  37.  
  38. if (snowfall.containsKey(userMonth)) {
  39. System.out.println("The hashmap already has data for this month");
  40.  
  41. // use scanner to ask user to overwrite or ignore?
  42. System.out.println("Do you want to overwrite? Enter 'yes' to overwrite");
  43. String overwrite = stringScanner.nextLine();
  44. if (overwrite.equals("yes")) {
  45. System.out.println("How much snow in " + userMonth + "?");
  46. int userSnow = numberScanner.nextInt();
  47. // Add this data to the HashMap.
  48. snowfall.put(userMonth, userSnow);
  49. }
  50.  
  51. }
  52.  
  53. else {
  54.  
  55. // Ask user for snow on that month
  56.  
  57. System.out.println("How much snow in " + userMonth + "?");
  58. int userSnow = numberScanner.nextInt();
  59. // Add this data to the HashMap.
  60.  
  61. snowfall.put(userMonth, userSnow);
  62. }
  63.  
  64.  
  65. // This is where I display ArrayList
  66. for (String month : snowfall.keySet()) {
  67. // month is a key
  68. System.out.println("Month: " + month);
  69. System.out.println("Snowfall inches: " + snowfall.get(month));
  70. }
  71. // This is where I define variable for snowfall total
  72. double sum = 0;
  73. //double month;
  74.  
  75. for (String month : snowfall.keySet()) {
  76. sum += snowfall.get(month);
  77. }
  78. System.out.println(sum);
  79.  
  80. // This is where I add up all the snowfall
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement