Java scanner class| import | nextline, nextint, exception and examples

Java Scanner class is found in found in the java.util package. Scanner class is used to getting the input of the primitive data types like Strings, int, double etc. It breaks the input using a pattern (regex), where the default regular expressions match is whitespace. This is the easiest way to get input in the java program.

Java scanner class, import nextline, nextint, exception and examples

This tutorial you will learn how to import a Scanner class in java, What are the use, How to use, important methods and many examples.

Java Scanner Syntax

A very easy syntax of Scanner class, after import java.util pakaage create a scanner object. Then use any method.

How to import Scanner Java

Java Import scanner class at the starting of the program inside the java file.

Java Scanner Example

Let’s See the simple Java scanner string reader. Where the input type is string read from a console.

Output: GIF with code structure. To use a scanner class in java, you have to import – import java.util package or import java.util.Scanner

Java Scanner Example output

Input Types in scanner

Above example was used Java scanner nextLine() methods, which is using for get string input.

But Java scanner class support all primitive type inputs. Read the below list supported input list data type with scanner methods. This is also called Input types in scanner calss.

Data TypeMethodDescription
booleannextBoolean()Read boolean value and returns that value.
bytenextByte()Read the input as a byte.
doublenextDouble()Read input as a double.
floatnextFloat()Read the input as a float.
intnextInt()Read the input as an int.
StringnextLine()Reads a String value from the user.
longnextLong()Read the input as a long.
shortnextShort()Read input as a short.

Example of different input types methods in Scanner class

Let’s see one by one method of Scanner class examples. We are not covering all types of methods, most are the same you can do it yourself. Every example has a code lines only other part like importing and mani methods is same above example.

Scanner nextInt

A Method is used for reading next input as a int date type.

Scanner nextLine

Java Scanner nextLine method used for reading string input type.

Java scanner nextDouble

A nextDouble method is used for racing a Double type data in java. Even you enter the int value in the console it will convert to double value.

Output: Enter a double value
0
value is: 0.0

Java scanner nextbyte

Scanner Class Exception

A scanner nextInt method will throw an error if you enter the string in a console. See the below example.

Note: Using a try-catch block can prevent – java scanner exception.

Output: See below GIF when you enter the number is fine but if string, it will throw an error – “java.util.InputMismatchException

java Scanner Class Exception error

Other Important method and examples

A hasNext() and next() methods are useful, see examples.

hasNext() Method

The Scanner hasnext method Returns true if this scanner has another token in its input. Using this method you can stop a loop or any condition statement in program.

Output:

next() Method

The Scanner next method finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. 

Output: Hello
World

How to Get Scanner next char?

Answer: Using Scanner.next() method you can take first or any character from the string.

How to Reading a .txt file using Scanner class in Java?

Answer: Java scanner file

This class introduced since Java version 1.6, so it’s most methods worked on the latest and old java version 8 etc. Do cooment if you have any doubt and suggestions.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All  Java scanner class is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.