Scanner and nextChar() in Java


Scanner is a class available in ‘java.util’ package used to take input from numerous sources like a file, or console. The most common source of taking input is from standard input i.e. keyboard. The scanner class has few methods to take input of primitives and strings from the keyboard.

We will understand scanner class and its various methods through this article.

Scanner and nextChar()

The following methods of Scanner class is used to take input −

  • nextInt() − To take integer as an input.

  • nextLong() − To take long as an input.

  • nextDouble() − To take double as an input.

  • nextFloat() − To take float as an input.

  • nextLine() − To take string as an input.

You may have noticed that there is no nextChar() method available in Scanner class. Generally, to take input we use ‘next’ followed by datatype name but that is not the case for character input. Instead of nextChar() we use next().charAt(0) to take input as character datatype.

We use both next() and charAt(0) methods simultaneously. The method next() can take input as string, or numbers but to restrict the input to single character datatype only we use charAt(0) with it. The argument 0 signifies single character.

First, let’s discuss how we can create Scanner and then we will see nextChar().

Syntax

Scanner nameOfobject = new Scanner(source);

Parameters

source − From where you want to read input.

Instance

Scanner read = new Scanner(System.in);

In the above example, we have created an object ‘read’ of Scanner class along with source ‘System.in’ which allows to read input from keyboard.

Once a Scanner is created we can use its object followed by dot(.) operator and its methods to take input from the specified sources and assign the values to the corresponding variable.

Example 1

The following example illustrates how we can use the scanner class and read input from standard input i.e. keyboard.

import java.util.Scanner;
public class Input {
   public static void main(String []args) {
      Scanner read = new Scanner(System.in);
      System.out.println("Enter integer value: ");
      int n1 = read.nextInt();
      System.out.println("Enter double value: ");
      double d1 = read.nextDouble();
      System.out.println("Enter float value: ");
      float f1 = read.nextFloat();
      System.out.println("Integer value: " + n1);
      System.out.println("Double value: " + d1);
      System.out.println("Float value: " + f1);
   }
}

Output

Enter integer value: 
9
Enter double value: 
236.555555
Enter float value: 
4.5
Integer value: 9
Double value: 236.555555
Float value: 4.5

In the above code, we have imported Scanner class from java.util package so that we can use Scanner in our program. ‘sc’ is the object of Scanner and ‘System.in’ is the source which is created to accept different primitive values from keyboard. We have used nextInt(), nextDouble() and nextFloat() to accept integer, double and float values respectively.

Example 2

In this example, we will use nextLine() to take string type input from keyboard and store the value in string type variable ‘st’.

import java.util.Scanner;
public class Input {
   public static void main(String []args) {
      Scanner read = new Scanner(System.in);
      System.out.println("Enter a String: ");
      while(read.hasNext()) {
         String st = read.nextLine();
         System.out.println("String value: " + st);
      }
   }
}

Output

Enter a String: 
tutorialspoint
String value: tutorialspoint

Example 3

In this example, we will use next().charAt(0) to take character type input from keyboard.

import java.util.Scanner;
public class Input{
   public static void main(String[] args){
      Scanner read = new Scanner(System.in);
      System.out.println("Enter any character value: ");
      char chs = read.next().charAt(0);
      System.out.println("The entered value: " + chs);
   }
}

Output

Enter any character value: 
T
The entered value: T

What if we use nextChar() to read character type input. Let’s see with an example.

Example 3

import java.util.Scanner;
public class Input {
   public static void main(String[] args) {
      Scanner read = new Scanner(System.in);
      System.out.println("Enter any character value: ");
      char chs = read.nextchar();
      System.out.println("The entered value: " + chs);
   }
}

Output

Input.java:6: error: cannot find symbol
char chs = read.nextchar();
                       ^
  symbol:   method nextchar()
  location: variable read of type Scanner
1 error

We are getting this error because nextChar() is not supported by java.

Conclusion

In Java, the scanner class supports nextInt(), nextFloat() but it doesn’t support nextChar(). Most of the new learners use nextChar() because it sounds similar to the other methods which are used to read input but they got error as nextChar() is not supported. Therefore, we have tried to explain the scanner class to clear all the doubts through this article.

Updated on: 05-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements