{"id":2689,"date":"2016-06-25T06:34:37","date_gmt":"2016-06-25T06:34:37","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2689"},"modified":"2021-03-21T09:56:20","modified_gmt":"2021-03-21T09:56:20","slug":"how-to-get-input-from-keyboard-using-scanner-class","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-get-input-from-keyboard-using-scanner-class\/","title":{"rendered":"How to get input from keyboard using Scanner class"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>In this tutorial, we will write a simple java program to get input from the keyboard using the Scanner class.<\/p>\n<h3>Import Scanner<\/h3>\n<p>We can import the Scanner class in the Java program using the below import statement. Alternatively, hover the mouse on the class name, Right-click, and select the import statement.<\/p>\n<p><strong>import java.util.Scanner;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2703\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-Java-Util-Class.png\" alt=\"Import Scanner Java Util Class\" width=\"1729\" height=\"1029\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-Java-Util-Class.png 1729w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-Java-Util-Class-300x179.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-Java-Util-Class-1024x609.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-Java-Util-Class-768x457.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-Java-Util-Class-1536x914.png 1536w\" sizes=\"auto, (max-width: 1729px) 100vw, 1729px\" \/><\/p>\n<h3><\/h3>\n<h3>Scanner class<\/h3>\n<p>A Scanner breaks the input entered by the user into tokens using a<br \/>\ndelimiter pattern, which by default matches whitespace. The resulting<br \/>\ntokens are then converted into different data types using the methods of the class.<\/p>\n<p><strong>Scanner keyboard = new Scanner(System.in);<\/strong><\/p>\n<p>System.in is the standard InputStream. The stream is open and ready to supply keyboard input data. Typically this depends on the source specified by<br \/>\nthe host environment or user.<\/p>\n<h3>Code Snippet<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner;\r\n\r\npublic class KeyboardInputDemo {\r\n\r\n  public static void main(String[] args)\r\n  {\tScanner keyboard = new Scanner(System.in);\r\n    System.out.print( \"Please enter your name :=\"  );\r\n    String name = keyboard.nextLine();\r\n    System.out.println( \"Output = Hello \" + name + \" !\" );\r\n    keyboard.close();\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3>Sample Output<\/h3>\n<p>Please enter your name :=TestingDocs<br \/>\nOutput = Hello TestingDocs !<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2700\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Keyboard-Input-Scanner-class-Java.png\" alt=\"Getting Keyboard Input Java\" width=\"1679\" height=\"1027\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Keyboard-Input-Scanner-class-Java.png 1679w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Keyboard-Input-Scanner-class-Java-300x184.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Keyboard-Input-Scanner-class-Java-1024x626.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Keyboard-Input-Scanner-class-Java-768x470.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Keyboard-Input-Scanner-class-Java-1536x940.png 1536w\" sizes=\"auto, (max-width: 1679px) 100vw, 1679px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Common mistake<\/h3>\n<p>Importing a different Scanner class and trying to run the program without resolving the compilation errors would result in Program error:<\/p>\n<p>Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problems:<br \/>\nScanner cannot be resolved to a type<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2689\" data-postid=\"2689\" class=\"themify_builder_content themify_builder_content-2689 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, we will write a simple java program to get input from the keyboard using the Scanner class. Import Scanner We can import the Scanner class in the Java program using the below import statement. Alternatively, hover the mouse on the class name, Right-click, and select the import statement. import java.util.Scanner; &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2689","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2689"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2689\/revisions"}],"predecessor-version":[{"id":19845,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2689\/revisions\/19845"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}