-1

i want to read inside a for loop two variables with scanner class then save them in a collection Map code is below :

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);
    }
  }
}

but i get this exeption after running the code :

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)

any help would be great thanks

azdoud
  • 1,094
  • 8
  • 15

2 Answers2

0

You need to validate your input to make sure that you get the type you expect. IT is not an int, so of course int sizeSubnet = scanner.nextInt(); is going to fail.

At the very least, a try/catch would be a good idea.

int sizeSubnet;
try{
    sizeSubnet = scanner.nextInt();
} catch () {
    sizeSubnet = 0;
}

If you expect that IT should be nameSubnet, then you'll need to make sure the scanner waits for the input with an extra scanner.nextLine();.

Community
  • 1
  • 1
senschen
  • 724
  • 10
  • 26
  • guys i want assign the nameSubnet first by String nameSubnet = scanner.nextLine(); that's why i gave it the string "IT" – azdoud Mar 28 '16 at 12:21
  • @azdoudyoussef then you should swap the inputs' places. – Dmitry Ginzburg Mar 28 '16 at 12:28
  • @azdoudyoussef That's why I updated the answer-- read the link to understand why you need to add the extra line. What's happening is that you aren't waiting on the user's input, so the first thing you type is not being read as the name, its being read as the size. – senschen Mar 28 '16 at 12:56
  • thanks you @senschen you've right Java scanner doesn't wait for my input, that's why SE throws that exception – azdoud Mar 28 '16 at 14:11
0

The reason of exception here is that scanner.nextInt(); returns an int,and at runtime you are passing IT which is type of java.lang.String. and an variable of type int can not store a String

Mehraj Malik
  • 11,335
  • 12
  • 46
  • 74
  • Is this Exception rising when you tack input for second time? – Mehraj Malik Mar 28 '16 at 12:26
  • This problem often rises when you try to take String input from console.Because the String termination symbol '\n' remains in the buffer.So when the controller come for the second time,Compiler think the '\n' is the input given by user and it takes '\n' as input,and dont wait for user input. Try to flush the buffer before taking 2nd input ,and you will not face this problem again. – Mehraj Malik Mar 28 '16 at 12:32
  • thanks friend @MehrajMalik yes it does not wait for user input this is a great explanation – azdoud Mar 28 '16 at 14:15