< 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 -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Anonymous Inner Class
Java Date Class >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2