Scanner Class in Java
In this article, We will see how to use scanner class in java with the help of some basic and easy java programs.
Java scanner class is a predefined class Which is used for reading the input or data from the keyboard given by the user.
Scanner class is a final class in java and it extends Object class and implements Closeable, AutoCloseable, and Iterator interfaces.
For example
public final class Scanner
extends Object
implements Iterator<String>, Closeable
Java scanner class is used for obtaining the input of primitive data types e.g int, double, etc. and String type.
Java scanner class is a predefined class Which is used for reading the input or data from the keyboard given by the user.
Scanner class is a final class in java and it extends Object class and implements Closeable, AutoCloseable, and Iterator interfaces.
For example
public final class Scanner
extends Object
implements Iterator<String>, Closeable
Java scanner class is used for obtaining the input of primitive data types e.g int, double, etc. and String type.
How to Import Scanner Class
Scanner class is predefined class which is available in java.util.* package.
If you want to use Scanner class in your java programs you have to import Scanner class in your programs.
java.util.Scanner;
If you want to use Scanner class in your java programs you have to import Scanner class in your programs.
java.util.Scanner;
Java Scanner Class Constructors
There are lots of constructors of scanner class but some of them are given below.
1) Scanner(java.io.File source)
Constructs a new scanner that produces values scanned from the specified path.
2) Scanner(java.io.InputStream source)
Constructs a new scanner that produces values scanned from the specified input stream.
3) Scanner(java.lang.readable source)
Constructs a new scanner that produces values scanned from the specified source.
4) Scanner(java.lang.String source)
Constructs a new scanner that produces values scanned from the specified string.
1) Scanner(java.io.File source)
Constructs a new scanner that produces values scanned from the specified path.
2) Scanner(java.io.InputStream source)
Constructs a new scanner that produces values scanned from the specified input stream.
3) Scanner(java.lang.readable source)
Constructs a new scanner that produces values scanned from the specified source.
4) Scanner(java.lang.String source)
Constructs a new scanner that produces values scanned from the specified string.
Methods of Scanner Class in Java
There are many methods defined in the Scanner class and by the help of scanner class method we can read the the input from the console or keyboard easily. Some Scanner class methods are given below.
1) public byte nextByte()
This method is used to read the data as byte value.
2) public short nextShort()
This method is used to read the data as short value.
3) public int nextInt()
This method is used to read the data as integer value.
4) public long nextLong()
This method is used to read the data as long value.
5) public float nextFloat()
This method is used to read the data as float value.
6) public double nextDouble()
This method is used to read the data as double value.
7) public char nextChar()
This method is used to read the data as character value.
8) public boolean nextBoolean()
This method is used to read the data as boolean value i.e true/false
9) public String nextLine()
This method reads any kind of data in the form of String.
10) public String next()
1) public byte nextByte()
This method is used to read the data as byte value.
2) public short nextShort()
This method is used to read the data as short value.
3) public int nextInt()
This method is used to read the data as integer value.
4) public long nextLong()
This method is used to read the data as long value.
5) public float nextFloat()
This method is used to read the data as float value.
6) public double nextDouble()
This method is used to read the data as double value.
7) public char nextChar()
This method is used to read the data as character value.
8) public boolean nextBoolean()
This method is used to read the data as boolean value i.e true/false
9) public String nextLine()
This method reads any kind of data in the form of String.
10) public String next()
Declaration of Scanner Class in Java
Before going to learn examples of Scanner class see the declaration of Scanner class.
Scanner sc = new Scanner(System.in);
Explanation:
Scanner(System.in) = This constructor creates an object of Scanner class with the object of InputStream class.
in = This is object of an InptutStream class.
System = System is a class and object 'in' is declared in System class as static data member.
System.in = Helps to read the input data from the console.
Let's see some Scanner class examples in java.
Let's see some Scanner class examples in java.
1) Scanner Class in Java Example
This is simple scanner class in java example where we will get input of int, double, types.
import java.util.Scanner;
class Demo1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your Age");
int age = sc.nextInt();
System.out.println("Your age is : "+age);
System.out.println("Enter your Marks");
double db = sc.nextDouble();
System.out.println("Your marks is : "+db);
}
}
Output: Enter your Age
50
Your age is : 50
Enter your Marks
65
Your marks is : 65.0
Output: Enter your Age
50
Your age is : 50
Enter your Marks
65
Your marks is : 65.0
2) Java Scanner Example With String
This is simple java scanner string example where we will learn how to take string input in java using scanner class.
import java.util.*;
class Demo2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("What is your first name");
String firstname = sc.next();
System.out.println("What is your last name");
String secondname = sc.next();
System.out.println("Your full name is : "+firstname+" "+secondname);
}
}
Output: What is your first name
Anurag
What is your last name
Singh
Your full name is : Anurag Singh
Read More:
Java Wrapper Classes Tutorial.
Java Abstract Class with example.
Java String Handling with example.
Here we discussed scanner class in java with the simple examples step-by-steps. I hope, you understand what is scanner class and some methods of scanner class in java.
Output: What is your first name
Anurag
What is your last name
Singh
Your full name is : Anurag Singh
Read More:
Java Wrapper Classes Tutorial.
Java Abstract Class with example.
Java String Handling with example.
Here we discussed scanner class in java with the simple examples step-by-steps. I hope, you understand what is scanner class and some methods of scanner class in java.
http://www.javatutorialpoint.com
ReplyDeleteI was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore
Great site and a great topic as well I really get amazed to read this.This is incredible,I feel really happy to have seen your webpage.I gained many unknown information, the way you have clearly explained is really fantastic.keep posting such useful information.
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course
Full Stack Training in Bangalore | Certification | Online Training Course
Full Stack Training in Hyderabad | Certification | Online Training Course
Full Stack Developer Training in Chennai | Mean Stack Developer Training in Chennai
Full Stack Training
Full Stack Online Training
it’s really nice and meanful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information.
ReplyDeleteDevOps Training in Chennai
DevOps Online Training in Chennai
DevOps Training in Bangalore
DevOps Training in Hyderabad
DevOps Training in Coimbatore
DevOps Training
DevOps Online Training
I'm really enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
I enjoying your post. You definitely have some great insight and great stories.keep it up!!
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
Thank you for the information. It is very useful and informative
ReplyDeleteangular js training in chennai
angular training in chennai
angular js online training in chennai
angular js training in bangalore
angular js training in hyderabad
angular js training in coimbatore
angular js training
angular js online training
very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Course in Chennai
SEO Training in Chennai
Digital Marketing Training in Bangalore
Digital Marketing Training in Hyderabad
Digital Marketing Training in Coimbatore
Digital Marketing Training
Digital Marketing Course
Digital Marketing Online Training
thanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!
ReplyDeleteWeb Designing Training in Chennai
Web Designing Course in Chennai
Web Designing Training in Bangalore
Web Designing Course in Bangalore
Web Designing Training in Hyderabad
Web Designing Course in Hyderabad
Web Designing Training in Coimbatore
Web Designing Training
Web Designing Online Training
it’s really nice and mean ful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them useful information.
ReplyDeleteAWS Course in Chennai
AWS Course in Bangalore
AWS Course in Hyderabad
AWS Course in Coimbatore
AWS Course
AWS Certification Course
AWS Certification Training
AWS Online Training
AWS Training
t’s really nice and meanful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information.
ReplyDeleteacte reviews
acte velachery reviews
acte tambaram reviews
acte anna nagar reviews
acte porur reviews
acte omr reviews
acte chennai reviews
acte student reviews
Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us.
ReplyDeletepython training in bangalore
python training in hyderabad
python online training
python training
python flask training
python flask online training
python training in coimbatore
Swift itself was not very hard to grasp. Many concepts were similar to JS or other languages, such as if statements and for loops. These are codes you write without depending on objects for iOS which was another learning curve all together. Swift was much stricter than JS.thanks!!
ReplyDeleteAndroid Training in Chennai
Android Online Training in Chennai
Android Training in Bangalore
Android Training in Hyderabad
Android Training in Coimbatore
Android Training
Android Online Training
Superb blog post! And this blog clearly explain about for useful information. I would Thanks for sharing this wonderful content.its very useful to us. Keep it up!
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Online Training in Chennai
Software Testing Courses in Chennai
Software Testing Training in Bangalore
Software Testing Training in Hyderabad
Software Testing Training in Coimbatore
Software Testing Training
Software Testing Online Training
Superb blog post! And this blog clearly explain about for useful information. I would Thanks for sharing this wonderful content.its very useful to us. Keep it up!
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Online Training in Chennai
Software Testing Courses in Chennai
Software Testing Training in Bangalore
Software Testing Training in Hyderabad
Software Testing Training in Coimbatore
Software Testing Training
Software Testing Online Training
thank you for this article ..very usefull to people ....
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course
Great Article android based projects
ReplyDeleteJava Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai
That was a great post.it was very useful.
ReplyDeleteJava Course in Nagpur