Monday, April 29, 2024
HomeJava10 Examples Of Scanner Class In Java

10 Examples Of Scanner Class In Java


Hey guys, if you wish to study Scanner class in Java, one of the crucial standard class to learn enter from command immediate or console then you’ve gotten come to the proper place. On this article, I’ll clarify find out how to learn enter from command immediate, what’s Scanner class and the way you should use Scanner class to learn varied knowledge varieties like String, int, lengthy, boolean immediately from console. You should have heard about well-known nextInt(), nextDouble() strategies which might learn integer and double values from command immediate or console. However, earlier than we get to the ten greatest examples that may educate you all the things you want to learn about scanner class in Java, let me let you know a little bit bit extra about what it truly is.

10 Examples Of Scanner Class In Java

In case you are at the least a little bit bit aware of Java, you could already learn about the usual enter and output strategies utilized by Java for studying and writing knowledge into commonplace units. However Java additionally provides you one other mechanism to learn consumer enter

This is called the scanner class. Although the scanner class shouldn’t be very environment friendly, it’s nonetheless one of many best and hottest methods to learn enter in Java packages.

The Scanner class can be utilized to scan and browse the enter of primitive knowledge inputs like decimal, int, and double. It would return a tokenized enter in line with some delimiter sample. For instance, if you wish to learn the sort dt, you should use the perform nextdt() for studying the enter.

1. Importing the Scanner

As
, the scanner class belongs to the java.util package deal. So in the event you
need to use the scanner class in your program, you want to import this
package deal. You need to use the next syntax:

import java.util.

You can even use the next syntax:

import java.util.Scanner;

2. Primary Instance of Java Scanner Class

You
can use the scanner to learn the inputs of a number of knowledge varieties. You’ll be able to
move the suitable predefined object to the scanner object if you’d like
to learn enter from a file or a channel. 

import java.util.*;  
public class Principal {  
    public static void predominant(String args[])    {  
                    Scanner in = new Scanner (System.in);  
                    System.out.print ("Enter a String: ");  
                    String mystr = in.nextLine();  
                    System.out.println("The String you entered is: " + mystr);             
                    in.shut();             
            }  
}  

3. Constructors And Strategies

The
scanner class is made up of assorted overloaded constructors that may
accommodate a number of enter strategies like file enter, path, and
System.in. 

  • Scanner(InputStream supply) will assemble a brand new scanner that may scan a brand new InputStream and produce the mandatory values. 
  • Scanner(File Supply) constructs a brand new scanner that may scan a specified file and produce the mandatory values. 
  • Scanner(String sources will assemble a brand new scanner that may scan a selected string and produce the values. 
  • Scanner(Path supply, string charsetName) constructs a brand new scanner that scans the required file and produces the values,
  • Scanner(ReadableByteChannel supply) constructs a brand new scanner that scans the required channel and produces the values.

4. Scanner Prototypes

  • Boolean hasNext() returns true if there may be one other token in Scanner’s enter.
  • Boolean hasNextBigInteger() checks if the subsequent token within the Scanner enter is of bigInteger kind.
  • Boolean hasNextByte() checks if the subsequent token within the Scanner enter is of kind Byte.
  • Boolean hasNextFloat() checks if the subsequent token within the Scanner enter is of float kind.
  • Boolean hasNextLine() checks if the subsequent token within the Scanner enter is one other line.
  • Boolean hasNextShort() checks if the subsequent token within the Scanner enter is of brief kind.
  • BigDecimal nextBigDecimal() scans the enter for subsequent BigDecimal token.
  • Boolean nextBoolean() scans the enter for the subsequent Boolean token.
  • Double nextDouble() scans the enter for the subsequent Double token.
  • Int nextInt() scans the enter for the subsequent integer token.
  • Lengthy nextLong() scans the enter for the subsequent Lengthy integer token.
  • Scanner reset() will reset the Scanner at present in use.

5. Utilizing The Scanner

The
implementation given under will present you find out how to use the scanner class
to learn enter from System.in. We are going to use a predefined System.in object
for making a scanner object. 

See the next program for extra info:

import java.util.*;
 
public class Principal{
     public static void predominant(String []args){
        String title;
        int myclass;
        float proportion;
         
        //creating object of Scanner class
        Scanner enter = new Scanner(System.in);
         
        System.out.print("Enter your title: ");
        title = enter.subsequent();
        System.out.print("Enter your class: ");
        myclass = enter.nextInt();
        System.out.print("Enter your proportion: ");
        proportion = enter.nextFloat();        
        enter.shut();
        System.out.println("Identify: " + title + ", Class: "+ myclass + ", Share: "
            + proportion);
     }
}

6. Scanner String

Utilizing
the scanner, you can even learn enter from strings. You need to use common
expressions contained in the string enter. Within the following program, the
scanner makes use of a string as an enter. See the instance to realize a greater
understanding:

import java.util.*;
 
public class Principal{
     public static void predominant(String []args){
          
        System.out.println ("The themes are as follows :");
        String enter = "1 Maths 2 English 3 Science 4 Hindi";
        Scanner s = new Scanner(enter);
        System.out.print(s.nextInt()+". ");
        System.out.println(s.subsequent());
        System.out.print(s.nextInt()+". ");
        System.out.println(s.subsequent());
        System.out.print(s.nextInt()+". ");
        System.out.println(s.subsequent());
        System.out.print(s.nextInt()+". ");
        System.out.println(s.subsequent());
        s.shut(); 
     }
}

7. Closing The Scanner

It
is fairly simple to shut the scanner. You need to use the Shut() methodology to
shut the scanner. It’s good to explicitly shut the scanner utilizing the
Shut() methodology when you find yourself performed utilizing it. 

8. Learn A Line Of Textual content Utilizing Scanner

import java.util.Scanner;

class Principal {
  public static void predominant(String[] args) {

    // creates an object of Scanner
    Scanner enter = new Scanner(System.in);

    System.out.print("Enter your title: ");

    // takes enter from the keyboard
    String title = enter.nextLine();

    // prints the title
    System.out.println("My title is " + title);

    // closes the scanner
    enter.shut();
  }
}

This program will provide you with the next output:

Enter your title: Kelvin

My title is Kelvin

8. Creating A Scanner Object

// learn enter from the enter stream
Scanner sc1 = new Scanner(InputStream enter);

// learn enter from recordsdata
Scanner sc2 = new Scanner(File file);

// learn enter from a string
Scanner sc3 = new Scanner(String str);

9. Java Scanner With nextInt()

import java.util.Scanner;

class Principal {
  public static void predominant(String[] args) {

    // creates a Scanner object
    Scanner enter = new Scanner(System.in);

    System.out.println("Enter an integer: ");

    // reads an int worth
    int data1 = enter.nextInt();

    System.out.println("Utilizing nextInt(): " + data1);

    enter.shut();
  }
}

You'll get the next output:

Enter an integer:
22
Utilizing nextInt(): 22

10. Java Scanner Utilizing nextDouble()

import java.util.Scanner;

class Principal {
  public static void predominant(String[] args) {

    // creates an object of Scanner
    Scanner enter = new Scanner(System.in);
    System.out.print("Enter Double worth: ");

    // reads the double worth
    double worth = enter.nextDouble();
    System.out.println("Utilizing nextDouble(): " + worth);

    enter.shut();
  }
}

This program will provide you with the next output:

Enter Double worth: 33.33
Utilizing nextDouble(): 33.33

That is all about find out how to use Scanner class in Java. We now have seen totally different instance of Scanner class for studying integer values utilizing nextInt(), double values utilizing nextDouble() in addition to many different instance of studying consumer enter from Console. Should you favored this listing of 10 examples of scanner class in Java, be at liberty to share it along with your family and friends.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments