Skip to content

Accepting input in Java: Scanner

October 5, 2013

A simple text scanner which can parse primitive types and strings using regular expressions.

Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods (ref).

For example, this code allows a user to read a number from

     Scanner sc = new Scanner(;
     int i = sc.nextInt();

Below are the way i usually use scanner ::

  1. –> imports the package where the Scanner  class is located
  3. –>  instantiate  Scanner to console object
  4. Scanner console = new Scanner(;
  5. –>creates the int,double,string, and char 
  6. int numInt;
  7. objects
  8. double numDouble;
  9. String str;
  10. char ch
  11. –> allows to use the method of  the scanner class. :: nextInt() only accepts Integers ::  nextDouble() accepts double :: next() accepts the input as  string :: and adding charAt() on the next() method accepts  the input only at its first character with 0 (zero)  indicating the index of the string.
  12. numInt = console.nextInt();
  13. numDouble = console.nextDouble();
  14. str =;
  15. ch =;

that’s all folks. also do you know any other way of using scanner?

I found other examples here

Related articles


From → Classes

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.


Look at what's brewing up guys! we all are work in progress. So don't meander, lets micromesh everything and have candid conversation about every problem related to C, C++ and JAVA. We can rejuvenate the crinkled faces.


A simple guide to programming...


Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans


Technology news, trends and analysis covering mobile, big data, cloud, science, energy and media

The Neighborhood

is under renovation in preparation for our 6th Season


News About Tech, Money and Innovation


The Diary of a Retiree

50 Year Project

My challenge to visit 192 countries, read 1,001 books, and watch the top 100 movies

[BTW] : Ben Trube, Writer

Writer, Programmer, Singer

My Arch Walkthrough

Arch linux made easy

Don Charisma

because anything is possible with Charisma

SutoCom Solutions

Success & Satisfaction with the Cloud

%d bloggers like this: