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 System.in:

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

Below are the way i usually use scanner ::

  1. –> imports the package where the Scanner  class is located
  2. import.java.util.*;
  3. –>  instantiate  Scanner to console object
  4. Scanner console = new Scanner(System.in);
  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 = console.next();
  15. ch = console.next().charAt(0);

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

I found other examples here

Related articles

Advertisements

From → Classes

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

TurtleAndRobot.com

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.

paritoshmishra

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.

ProgramThat;

A simple guide to programming...

ITechonology

Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans

Gigaom

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

The Neighborhood

telling the story from every vantage point

HarsH ReaLiTy

A Good Blog is Hard to Find

VentureBeat

News About Tech, Money and Innovation

retireediary

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: