无法将HashMap键值与Scanner输入进行比较

时间:2014-09-29 04:54:26

标签: java hashmap

我试图将HashMap中的键值与用户输入生成的数组的索引进行比较。总体目标是将2000年12月25日变为2000年12月25日,除此之外其他一切都有效。

import java.util.*;

class Dates {

private static final Set<Integer> THIRTY_DAY_MONTHS = new HashSet<>(Arrays.asList(4, 6, 8, 11));
private static Map<Integer, String> MONTHS = new HashMap<>(12);
private static String monthString;

public static void main(String[] args) {
    try {
        System.out.println("Enter month/day/year to be converted to normal format: ");
        Scanner s = new Scanner(System.in);

        String[] input = s.nextLine().split("/");

        int[] inputNumbers = new int[input.length];
        for (int i = 0; i < input.length; i++) {
            inputNumbers[i] = Integer.parseInt(input[i]);
        }

        int month = inputNumbers[0];
        int day = inputNumbers[1];
        int year = inputNumbers[2];

        MONTHS.put(1, "January");
        MONTHS.put(2, "February");
        MONTHS.put(3, "March");
        MONTHS.put(4, "April");
        MONTHS.put(5, "May");
        MONTHS.put(6, "June");
        MONTHS.put(7, "July");
        MONTHS.put(8, "August");
        MONTHS.put(9, "September");
        MONTHS.put(10, "October");
        MONTHS.put(11, "November");
        MONTHS.put(12, "December");

        for(Integer currentKey : MONTHS.keySet()) {
            if (currentKey == month)
                monthString = MONTHS.get(currentKey);
            else
                throw new MonthException();
        }

        if (THIRTY_DAY_MONTHS.contains(month) && day > 30) { //Logic for valid day ranges for months that only contain 30 days
            throw new DayException();
        } else if (day > 31 || day <= 0) { //Logic for max and min # of possible days per any month
            throw new DayException();
        } else if (month == 2 && day > 29 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { //Logic for # of days in February for a leap year
            throw new DayException();
        } else if (year < 1000 || year > 3000) { //Logic for valid range of years
            throw new YearException();
        }

        System.out.println(monthString + ' ' + day + ", " + year);


    } catch (MonthException | DayException | YearException e) {
        System.out.println(e.getMessage());
    }
  }
}     

我将12/25/2000这样的东西分成12,25和2000整数,然后将它们设置为monthdayyear。但是,当我尝试将currentKey与月份值进行比较时,它总是会抛出MonthException,我不知道为什么。我尝试使用entrySet来获取键和值,但我得到了相同的结果。

我想要发生的是循环迭代,直到currentKey等于输入的month,然后将monthString设置为该键的值。

5 个答案:

答案 0 :(得分:1)

你可以用

替换for循环
monthString = MONTHS.get(month);
if (monthString == null) {
    throw new MonthException();
}

其中monthString为null,如果在Map中找不到该键,则触发MonthException。

答案 1 :(得分:0)

只看你的代码

for(Integer currentKey : MONTHS.keySet()) {
    if (currentKey == month) // this line may cause the problem
        monthString = MONTHS.get(currentKey);
    else
        throw new MonthException();
}

你应该使用

if(currentKey.equals(month)) 

为什么需要for循环播放?

你可以使用

monthString = MONTHS.get(month);

现在如果monthString=null您可以抛出异常。

monthString = MONTHS.get(month);
if (monthString == null) {
 throw new MonthException();
}

答案 2 :(得分:0)

如果谓词失败,则else块在第一次迭代时运行。你想在通过整个地图循环后才抛出异常。

for(Integer currentKey : MONTHS.keySet()) {
    if (currentKey == month) {
        monthString = MONTHS.get(currentKey);
        break;
    }
    else if (currentKey != month && currentKey == 12)
        throw new MonthException();
}

答案 3 :(得分:0)

没有必要循环一个地图很慢的地图。 你可以使用像这样快速的containsKey方法:

    if(MONTHS.containsKey(month))
    {
        monthString = MONTHS.get(month);
    }
    else
    {
        throw new MonthException();
    }

答案 4 :(得分:-1)

抛出异常的原因,它打破了循环;

更改代码,并在循环后添加throws子句,如...

   /**      after you code...  **/
    boolean notfound=true;
    for (Integer currentKey : MONTHS.keySet()) {
        String monthString;
        System.out.println("test.main()"+currentKey+month);
        if (currentKey.equals(month)){
         monthString = MONTHS.get(currentKey);
         notfound=false;


    }

}
    if(notfound)            
        throw new Exception();