Java

Java – Scanner Class With Examples

As seen below, we may overload a method with the varargs argument. Here, a method sum has been overloaded (). This method’s first variant supports variable int inputs and outputs their total. The concatenated text is printed by the second iteration of the sum() function, which also takes variable string parameters.

Example 1: Read user input text using Scanner

Let’s go through everything in depth since this is the first instance of the Scanner class.
1. The opening line imports java.util.
When use the Scanner class, a scanner is required. As an alternative, you may import Scanner using the syntax java.util.*, which will import any class included in the java.util package.
2. We gave System.in to the constructor of the Scanner class when constructing an instance of that class. To read the data from standard input, do this.
3. To read the line of text supplied by the user, we utilized the nextLine() function of the Scanner class.

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {

    // creating a scanner
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter your first name: ");

    // read user input and store it in a string variable
    String firstName = scan.nextLine();

    System.out.print("Enter your last name: ");

    // read second input
    String lastName = scan.nextLine();

    // prints the user entered data
    System.out.println("Your Name is: "+firstName+" "+lastName);

    // close scanner
    scan.close();
  }
}

Output:
Java Scanner with examples

Java Scanner class methods

We have seen the usage of the nextLine() function in the example above. In this class, there are several methods accessible. Here is a list of some of the Scanner class’s most utilized methods.

METHOD DESCRIPTION
nextInt() It reads an int value entered by the user
nextFloat() It reads a float value entered by the user
nextBoolean() It reads a boolean value entered by the user
nextLine() It reads a line of text entered by the user
next() This method reads a word entered by the user
hasNextLine() Checks if there is another line of text entered by the user
hasNextInt() Checks if there is another int value input by the user
reset() It resets the scanner
toString() It is used to get string representation of user input
nextByte() This method reads a byte value entered by the user
nextDouble() This method reads a double value entered by the user
nextShort() It reads a short value entered by the user
nextLong() It reads a long value entered by the user

Example 2: Java Scanner nextInt() method

We are using this example to show how to utilize the nextInt() function. The integer value supplied by the user is read using this method. To get two numbers from the user, we utilize the nextInt() function twice. After that, we output the total of the entered numbers.

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {

    // creating a scanner
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter first number: ");

    // read user input and store it in an int variable
    int num1 = scan.nextInt();

    System.out.print("Enter second number: ");

    // read second input
    int num2 = scan.nextInt();

    // prints the sum of entered numbers
    System.out.print("Sum of entered numbers: "+(num1+num2));

    // close scanner
    scan.close();
  }
}

Output:
Java Scanner example

Example 3: Java Scanner next() method

The next() function differs from nextLine() in many ways. The next() function reads the user-entered word instead of the line of text as it does with the nextLine() method. The next() function examines the input up until it encounters a whitespace. After a whitespace, the user input is not read.

In the example that follows, the user is requested to provide their entire name, but only the first name was recorded since the next() function stopped processing the input as soon as it saw a whitespace. The identical example will be used once again using the nextLine() function to get the desired results.

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {

    // creating a scanner
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter your full name: ");

    // read the user input using next() method
    // This method only reads a word, which means
    // if there is a whitespace between words, the
    // first word is scanned and second skipped
    String name = scan.next();

    System.out.print("You name: "+name);

    // close scanner
    scan.close();
  }
}

Output:
Java Scanner example

Example 4: Java Scanner nextLine() method

Let’s use the same example once again. As you can see, we can read the whole user input by using nextLine(). This is so that a whole line may be read using this way.

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {

    // creating a scanner
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter your full name: ");

    // The nextLine() method works different from
    // the next() method, unlike next(), it reads the
    // complete line
    String name = scan.nextLine();

    System.out.print("You name: "+name);

    // close scanner
    scan.close();
  }
}

Output:
Java Scanner example

Example 5: Java Scanner useDelimiter() Method

A delimiter character in the input may be specified using the userDelimiter() function. In the example below, we have designated the character “/” as a delimiter. We also use the hasNext() function of the Scanner class to read input that has been divided by a delimiter.

import java.util.Scanner;
public class JavaExample {
  public static void main(String args[]){
    // Initializing a Scanner object
    Scanner scan = new Scanner("BeginnersBook/Chaitanya/Website");

    //Initialize the delimiter in useDelimiter() method
    scan.useDelimiter("/");

    //printing the strings separated by delimiter
    while(scan.hasNext()){
      System.out.println(scan.next());
    }
    scan.close();
  }
}

Output:

BeginnersBook
Chaitanya
Website

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button