Advertisement



< Prev
Next >



Scanner Class





//Exception while reading through the Scanner class.

import java. util.*;

class A
{
public static void main(String... ar)
{
Scanner s = new Scanner(System.in); 		//reading user inputs from the keyboard.

while(s.hasNext())
{
if(s.hasNextInt())
{
System.out.println("Int is " + s.nextBoolean()); //trying to convert an int value to a boolean value
}

else if(s.hasNext())
System.out.println("String "+ s.next());
}

}
}


Output -


12
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.nextBoolean(Unknown Source)
        at A.main(Scanner6.java:16)


Program Analysis


In the preceding code, we are reading LIVE user inputs from the keyboard and as soon as user entered an int value of 12.



Please share this article -




< Prev
Next >
< Anonymous Inner Class
Java Date Class >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement