In nextint java что это
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter

# Scanner
# General Pattern that does most commonly asked about tasks
The following is how to properly use the java.util.Scanner class to interactively read user input from System.in correctly( sometimes referred to as stdin , especially in C, C++ and other languages as well as in Unix and Linux). It idiomatically demonstrates the most common things that are requested to be done.
# Using custom delimiters
You can use custom delimiters (regular expressions) with Scanner, with .useDelimiter(",") , to determine how the input is read. This works similarly to String.split(. ) . For example, you can use Scanner to read from a list of comma separated values in a String:
This will allow you to read every element in the input individually. Note that you should not use this to parse CSV data, instead, use a proper CSV parser library, see CSV parser for Java
(opens new window) for other possibilities.
# Reading system input using Scanner
The scanner object is initialized to read input from keyboard. So for the below input from keyboar, it’ll produce the output as Reading from keyboard
# Reading file input using Scanner
Here a Scanner object is created by passing a File object containing the name of a text file as input. This text file will be opened by the File object and read in by the scanner object in the following lines. scanner.hasNext() will check to see if there is a next line of data in the text file. Combining that with a while loop will allow you to iterate through every line of data in the Names.txt file. To retrieve the data itself, we can use methods such as nextLine() , nextInt() , nextBoolean() , etc. In the example above, scanner.nextLine() is used. nextLine() refers to the following line in a text file, and combining it with a scanner object allows you to print the contents of the line. To close a scanner object, you would use .close() .
Using try with resources (from Java 7 onwards), the above mentioned code can be written elegantly as below.
# Read the entire input as a String using Scanner
You can use Scanner to read all of the text in the input as a String, by using \Z (entire input) as the delimiter. For example, this can be used to read all text in a text file in one line:
Remember that you’ll have to close the Scanner, as well as catch the IoException this may throw, as described in the example Reading file input using Scanner
# Read an int from the command line
If you want to read an int from the command line, just use this snippet. First of all, you have to create a Scanner object, that listens to System.in, which is by default the Command Line, when you start the program from the command line. After that, with the help of the Scanner object, you read the first int that the user passes into the command line and store it in the variable number. Now you can do whatever you want with that stored int.
# Carefully Closing a Scanner
it can happen that you use a scanner with the System.in as parameter for the constructor, then you need to be aware that closing the scanner will close the InputStream too giving as next that every try to read the input on that (Or any other scanner object) will throw an java.util.NoSuchElementException or an java.lang.IllegalStateException
# Syntax
- Scanner scanner = new Scanner(Source source);
- Scanner scanner = new Scanner(System.in);
# Parameters
| Parameter | Details |
|---|---|
| Source | Source could be either one of String, File or any kind of InputStream |
# Remarks
The Scanner class was introduced in Java 5. The reset() method was added in Java 6, and a couple of new constructors were added in Java 7 for interoperability with the (then) new Path interface.
Class Scanner
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
For example, this code allows a user to read a number from the console.
As another example, this code allows long types to be assigned from entries in a file myNumbers :
The scanner can also use delimiters other than whitespace. This example reads several items in from a string:
prints the following output:
The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:
The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace() . The reset() method will reset the value of the scanner’s delimiter to the default whitespace delimiter regardless of whether it was previously changed.
A scanning operation may block waiting for input.
The next() and hasNext() methods and their companion methods (such as nextInt() and hasNextInt() ) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext() and next() methods may block waiting for further input. Whether a hasNext() method blocks has no connection to whether or not its associated next() method will block. The tokens() method may also block waiting for input.
The findInLine() , findWithinHorizon() , skip() , and findAll() methods operate independently of the delimiter pattern. These methods will attempt to match the specified pattern with no regard to delimiters in the input and thus can be used in special circumstances where delimiters are not relevant. These methods may block waiting for more input.
When a scanner throws an InputMismatchException , the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
Depending upon the type of delimiting pattern, empty tokens may be returned. For example, the pattern «\\s+» will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern «\\s» could return empty tokens since it only passes one space at a time.
A scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable’s read() method throws an IOException then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method.
When a Scanner is closed, it will close its input source if the source implements the Closeable interface.
A Scanner is not safe for multithreaded use without external synchronization.
Unless otherwise mentioned, passing a null parameter into any method of a Scanner will cause a NullPointerException to be thrown.
A scanner will default to interpreting numbers as decimal unless a different radix has been set by using the useRadix(int) method. The reset() method will reset the value of the scanner’s radix to 10 regardless of whether it was previously changed.
Localized numbers
An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner’s locale. A scanner’s initial locale is the value returned by the Locale.getDefault(Locale.Category.FORMAT) method; it may be changed via the useLocale() method. The reset() method will reset the value of the scanner’s locale to the initial locale regardless of whether it was previously changed.
The localized formats are defined in terms of the following parameters, which for a particular locale are taken from that locale’s DecimalFormat object, df , and its and DecimalFormatSymbols object, dfs .
Class Scanner
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
For example, this code allows a user to read a number from System.in :
As another example, this code allows long types to be assigned from entries in a file myNumbers :
The scanner can also use delimiters other than whitespace. This example reads several items in from a string:
prints the following output:
The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:
The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace() . The reset() method will reset the value of the scanner’s delimiter to the default whitespace delimiter regardless of whether it was previously changed.
A scanning operation may block waiting for input.
The next() and hasNext() methods and their companion methods (such as nextInt() and hasNextInt() ) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext() and next() methods may block waiting for further input. Whether a hasNext() method blocks has no connection to whether or not its associated next() method will block. The tokens() method may also block waiting for input.
The findInLine() , findWithinHorizon() , skip() , and findAll() methods operate independently of the delimiter pattern. These methods will attempt to match the specified pattern with no regard to delimiters in the input and thus can be used in special circumstances where delimiters are not relevant. These methods may block waiting for more input.
When a scanner throws an InputMismatchException , the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
Depending upon the type of delimiting pattern, empty tokens may be returned. For example, the pattern «\\s+» will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern «\\s» could return empty tokens since it only passes one space at a time.
A scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable’s read() method throws an IOException then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method.
When a Scanner is closed, it will close its input source if the source implements the Closeable interface.
A Scanner is not safe for multithreaded use without external synchronization.
Unless otherwise mentioned, passing a null parameter into any method of a Scanner will cause a NullPointerException to be thrown.
A scanner will default to interpreting numbers as decimal unless a different radix has been set by using the useRadix(int) method. The reset() method will reset the value of the scanner’s radix to 10 regardless of whether it was previously changed.
Localized numbers
An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner’s locale. A scanner’s initial locale is the value returned by the Locale.getDefault(Locale.Category.FORMAT) method; it may be changed via the useLocale() method. The reset() method will reset the value of the scanner’s locale to the initial locale regardless of whether it was previously changed.
The localized formats are defined in terms of the following parameters, which for a particular locale are taken from that locale’s DecimalFormat object, df , and its and DecimalFormatSymbols object, dfs .