首页 > 解决方案 > 循环中的java输入给出不匹配异常

问题描述

这是下面的代码和它的输入,它显示第二个循环不匹配,有人可以解释为什么

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class MapDemo {

    public static void main(String[] args) {
        HashMap<Integer, String> hmap = new HashMap<>();
        Scanner in = new Scanner(System.in);

        for (int i = 0; i < 3; i++) {
            Integer a = in.nextInt();
            String b = in.next();

            hmap.put(a, b);
        }

        for (Map.Entry<Integer, String> m : hmap.entrySet()) {
            System.out.println(m.getKey() + " " + m.getValue());
        }
    }
}

输入:

10654
Max Bupa Health Insurance
10321
SBI Health Insurance
20145
IFFCO Tokio Two Wheeler Insurance

标签: javahashmapinputmismatchexception

解决方案


推荐阅读