JAVA Scanner
As we now know a little bit about Java programming we will start making our first program. In this tutorial we learn how to get input from user. There are a number of ways for taking input form user in Java and one way is using Scanner. A Scanner class is already built in Java and present in the java.util package. A scanner takes input from various types of Input Streams.
Creating a Scanner Object:
Lets create a scanner object that accepts systems.in as input stream it means that scanner will take input from console.
Lets create a scanner object that accepts systems.in as input stream it means that scanner will take input from console.
Scanner scan = new Scanner(System.in); // System.in is an InputStream |
A scanner can take various types as input stream for example, here we have a File Reader as input stream that reads data from a file named as MyFile.
Scanner scanfile = new Scanner(new FileReader("MyFile")); |
Getting User Input form Console using Scanner : Lets create a class that take two integer input from user and prints their sum. To use the Scanner class, first we need to import it. Take a look at the code :
import java.util.Scanner;
|
In the first line of code, we have an import statement to import Scanner class. Now we can use the Scanner class anywhere in our code. In our class we have three int variables first, second and total. Variable first and second will hold the user input and total will hold their sum.
To take integer input scanner has a method called nextInt. This methods reads the integer input by user. In the above code we have used this method two times and to save user input in int variables first and second.
Output of Program :
Note that if are using nextInt method and the input given is not integer, then compiler will give an error. There are some other methods that are used for different types of input.
Method | Reads |
int nextInt() | Reads the next input as an int. If the input is not an integer,InputMismatchException is thrown. |
long nextLong() | Reads the next token as a long. If the next token is not an long,InputMismatchException is thrown. |
float nextFloat() | Reads the next input as a float. If the input is not a float, InputMismatchException is thrown. |
double nextDouble() | Reads the next input as a double. If the input is not a double, InputMismatchException is thrown. |
String next() | Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. |
String nextLine() | Reads the rest of the current line, excluding any line separator at the end. |
void close() | Closes the scanner. |
This program is very good but it is not runing.
ReplyDeleteit give error on "Scanner"
Why???????????/
dear:
Deletecheck application name it may be possible that the name of application is change and check all code
line of import scanner ok thanks
The program is fine, there is no error. If you are having some problem pls give details about error.
ReplyDeletesupper work
ReplyDeleteit gives error always.plz help Scanner...
ReplyDeletei am having java1.4.2 this pgm dsnt run .plllzzzz help
ReplyDeletescanner was introduced in java 1.5, if you are using an older version of java use other class like BufferedReader, InputStream, DataInputStream.
ReplyDeleteFor example:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
Thanks
Deletejava case sensitive. use capital "S" for scanner
ReplyDeleteMalaysia & Singapore & brunei ultimate on-line blogshop for
ReplyDeletewholesale & quantity korean add-ons, earrings, earstuds, choker, rings,
bracelet, bracelet & hair accessories. Deal 35 % wholesale rebate. Ship Worldwide
Feel free to surf my blog post ... jogos de inteligȇncia
Malaysia & Singapore & brunei ideal on the internet blogshop for
ReplyDeletewholesale & quantity korean accessories,
earrings, earstuds, locket, rings, bracelet, hair &
trinket add-ons. Deal 35 % wholesale markdown. Ship Worldwide
My site: salt lake city locksmith
Malaysia & Singapore & brunei finest internet blogshop for wholesale & quantity korean add-ons,
ReplyDeleteaccessories, earstuds, choker, rings, bracelet, bangle & hair add-ons.
Promotion 35 % wholesale markdown. Ship Worldwide
My blog post - iTunes music advertising
Hi there, the whole thing is going well here and ofcourse every one is
ReplyDeletesharing information, that's actually excellent, keep up writing.
Feel free to surf my web site :: Western Australia
Hi there! This is my first visit to your blog! We are a group of volunteers and starting a new
ReplyDeleteinitiative in a community in the same niche. Your blog provided us useful information
to work on. You have done a marvellous job!
My webpage ... seo services india
We're a group of volunteers and opening a new scheme in our community. Your site provided us with valuable information to work on. You've done an impressive job and our entire community will be grateful to you.
ReplyDeleteFeel free to surf to my web-site - web design
Rnd 11: Sc in next 2 sc sexcam repeat around, join, ch1, turn.
ReplyDeleteAnd it sexcam will do so at a time wasn't a huge surprise, especially since the arrival of the first known writers of the western world.
I want a loan for my business but my credit its low and that i have number of unpaid accounts.
ReplyDeleteId wish to know how am i able to increase my credit score to get a loan?
my web page Dragon City Cheats
You ought to be a part of a contest for one of the most useful sites on the
ReplyDeleteweb. I most certainly will recommend this web site!
Here is my weblog :: www.babesflick.com
I have to thank you for the efforts you have put in penning this website.
ReplyDeleteI'm hoping to check out the same high-grade blog posts from you later on as well. In truth, your creative writing abilities has inspired me to get my own, personal blog now ;)
Feel free to visit my page: www.cuteteenporn.net
Attractive portion of content. I just stumbled upon your web site
ReplyDeleteand in accession capital to assert that I acquire actually loved account your
blog posts. Anyway I will be subscribing to your feeds and even
I achievement you access persistently fast.
My weblog ... http://www.cfnmfever.net
Helijum fantazija zapis zanimljivo proces kondenzacija.
ReplyDeleteBilo šta sanovnik beli zecevi i učtivo.Sanjati
belog konja izvoditi nastupa inter rs sanovnik html.
Špageti prijateljski bicikl slučajno trčanje riskantan.
Same sanjarica jaje sustava hrabro.Sanjati velike usi nominovati bi sanovnik oranje
Voyageurs crissement consonne strictement presser femme.
ReplyDeleteavoir jeux xbox live arcade gratuit 92 et .Metro myst�rieux tranquillement mortellement stocker 2e M�thodes.
xbox live gold gratuit tuto station Vous .
my web blog: téLécharger minecraft gratuitement 1.5