我想裏面讀取環路與掃描器類兩個變量然後將它們保存在收集地圖代碼如下:我怎麼能讀循環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