Monday 12 March 2018

How To Use Scanner Class In Java ~ foundjava

Scanner Class in Java

How to use Scanner Class in Java ~ foundjava

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.


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;


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.


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()


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.


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


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

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.

No comments:

Post a Comment