我想里面读取环路与扫描器类两个变量然后将它们保存在收集地图代码如下:我怎么能读循环java的两个输入
public class Example{
public static void main(String args[]){
Map<String,Integer> mapSub = new HashMap<String,Integer>();
for (int i=0;i<nbSubnet;i++){
System.out.println("Enter name of the subnet "+i+" : ");
String nameSubnet = scanner.nextLine();
System.out.println("Enter the size of the subnet "+i+" : ");
int sizeSubnet = scanner.nextInt();
mapSub.put(nameSubnet, sizeSubnet);
}
}
}
但在运行后,我得到这个exeption验证码:
Enter name of the subnet 0 :
Enter the size of the subnet 0 :
IT
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at view.Main.main(Main.java:60)
任何帮助将是巨大的感谢
据我所知,'IT'不是'int' – Hackerdarshi
我给它的字符串“IT”作为名字,是我从代码做期待,请问串首先,然后要求的整数大小 – azdoud
我认为这将有助于http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – RubioRic