Let's see a simple example of Java Scanner where we are getting a single input from the user. Here, we are asking for a string through in. JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week.
Java Serialization Java transient keyword. Next Topic PrintStream class. Reinforcement Learning. R Programming.
React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence.
Cloud Computing. Data Science. Angular 7. You can read more about exceptions and how to handle errors in the Exceptions chapter. We just launched W3Schools videos. Get certified by completing a course today! If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:.
Example import java. Method Description nextBoolean Reads a boolean value from the user nextByte Reads a byte value from the user nextDouble Reads a double value from the user nextFloat Reads a float value from the user nextInt Reads a int value from the user nextLine Reads a String value from the user nextLong Reads a long value from the user nextShort Reads a short value from the user.
Report Error. 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. Locale 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.
LocalGroupSeparator The character used to separate thousands groups, i. DecimalNumeral Exponent? Since: 1. Scanner File source, String charsetName Constructs a new Scanner that produces values scanned from the specified file. Scanner InputStream source Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner InputStream source, String charsetName Constructs a new Scanner that produces values scanned from the specified input stream. Scanner Path source Constructs a new Scanner that produces values scanned from the specified file. Scanner Path source, String charsetName Constructs a new Scanner that produces values scanned from the specified file. Scanner Readable source Constructs a new Scanner that produces values scanned from the specified source.
Scanner ReadableByteChannel source Constructs a new Scanner that produces values scanned from the specified channel. Scanner ReadableByteChannel source, String charsetName Constructs a new Scanner that produces values scanned from the specified channel.
Scanner String source Constructs a new Scanner that produces values scanned from the specified string. Pattern delimiter Returns the Pattern this Scanner is currently using to match delimiters. String findInLine Pattern pattern Attempts to find the next occurrence of the specified pattern ignoring delimiters. String findInLine String pattern Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
String findWithinHorizon Pattern pattern, int horizon Attempts to find the next occurrence of the specified pattern. String findWithinHorizon String pattern, int horizon Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
Locale locale Returns this scanner's locale. MatchResult match Returns the match result of the last scanning operation performed by this scanner. String next Finds and returns the next complete token from this scanner. String next Pattern pattern Returns the next token if it matches the specified pattern. String next String pattern Returns the next token if it matches the pattern constructed from the specified string.
String nextLine Advances this scanner past the current line and returns the input that was skipped. Scanner reset Resets this scanner. Scanner skip Pattern pattern Skips input that matches the specified pattern, ignoring delimiters. Scanner skip String pattern Skips input that matches a pattern constructed from the specified string. String toString Returns the string representation of this Scanner. Scanner useDelimiter Pattern pattern Sets this scanner's delimiting pattern to the specified pattern.
Scanner useDelimiter String pattern Sets this scanner's delimiting pattern to a pattern constructed from the specified String. Scanner useLocale Locale locale Sets this scanner's locale to the specified locale. Scanner useRadix int radix Sets this scanner's default radix to the specified radix. Methods inherited from class java. Object clone , equals , finalize , getClass , hashCode , notify , notifyAll , wait , wait , wait Constructor Detail Scanner public Scanner Readable source Constructs a new Scanner that produces values scanned from the specified source.
Parameters: source - A character source implementing the Readable interface Scanner public Scanner InputStream source Constructs a new Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the underlying platform's default charset.
Parameters: source - An input stream to be scanned Scanner public Scanner InputStream source, String charsetName Constructs a new Scanner that produces values scanned from the specified input stream.
Bytes from the stream are converted into characters using the specified charset. Parameters: source - An input stream to be scanned charsetName - The encoding type used to convert bytes from the stream into characters to be scanned Throws: IllegalArgumentException - if the specified character set does not exist Scanner public Scanner File source throws FileNotFoundException Constructs a new Scanner that produces values scanned from the specified file.
Bytes from the file are converted into characters using the underlying platform's default charset. Bytes from the file are converted into characters using the specified charset. Parameters: source - A file to be scanned charsetName - The encoding type used to convert bytes from the file into characters to be scanned Throws: FileNotFoundException - if source is not found IllegalArgumentException - if the specified encoding is not found Scanner public Scanner Path source throws IOException Constructs a new Scanner that produces values scanned from the specified file.
Parameters: source - A string to scan Scanner public Scanner ReadableByteChannel source Constructs a new Scanner that produces values scanned from the specified channel. Bytes from the source are converted into characters using the underlying platform's default charset. Parameters: source - A channel to scan Scanner public Scanner ReadableByteChannel source, String charsetName Constructs a new Scanner that produces values scanned from the specified channel.
Bytes from the source are converted into characters using the specified charset. Parameters: source - A channel to scan charsetName - The encoding type used to convert bytes from the channel into characters to be scanned Throws: IllegalArgumentException - if the specified character set does not exist Method Detail close public void close Closes this scanner.
If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked.
If this scanner is already closed then invoking this method will have no effect. Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException. This method returns null if no such exception exists. Returns: the last exception thrown by this scanner's readable delimiter public Pattern delimiter Returns the Pattern this Scanner is currently using to match delimiters.
Returns: this scanner's delimiting pattern. Parameters: pattern - A delimiting pattern Returns: this scanner useDelimiter public Scanner useDelimiter String pattern Sets this scanner's delimiting pattern to a pattern constructed from the specified String. An invocation of this method of the form useDelimiter pattern behaves in exactly the same way as the invocation useDelimiter Pattern. Invoking the reset method will set the scanner's delimiter to the default.
Parameters: pattern - A string specifying a delimiting pattern Returns: this scanner locale public Locale locale Returns this scanner's locale. A scanner's locale affects many elements of its default primitive matching regular expressions; see localized numbers above.
Returns: this scanner's locale useLocale public Scanner useLocale Locale locale Sets this scanner's locale to the specified locale. Invoking the reset method will set the scanner's locale to the initial locale. Parameters: locale - A string specifying the locale to use Returns: this scanner radix public int radix Returns this scanner's default radix.
A scanner's radix affects elements of its default number matching regular expressions; see localized numbers above. Returns: the default radix of this scanner useRadix public Scanner useRadix int radix Sets this scanner's default radix to the specified radix. If the radix is less than Character. Invoking the reset method will set the scanner's radix to Parameters: radix - The radix to use when scanning numbers Returns: this scanner Throws: IllegalArgumentException - if radix is out of range match public MatchResult match Returns the match result of the last scanning operation performed by this scanner.
This method throws IllegalStateException if no match has been performed, or if the last match was not successful. The various next methods of Scanner make a match result available if they complete without throwing an exception. For instance, after an invocation of the nextInt method that returned an int, this method returns a MatchResult for the search of the Integer regular expression defined above.
Similarly the findInLine java. Pattern methods will make a match available if they succeed. Returns: a match result for the last match operation Throws: IllegalStateException - If no match result is available toString public String toString Returns the string representation of this Scanner.
The string representation of a Scanner contains information that may be useful for debugging. The exact format is unspecified. Overrides: toString in class Object Returns: The string representation of this scanner hasNext public boolean hasNext Returns true if this scanner has another token in its input.
This method may block while waiting for input to scan. The scanner does not advance past any input. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext returned true. See Also: Iterator hasNext public boolean hasNext String pattern Returns true if the next token matches the pattern constructed from the specified string.
An invocation of this method of the form hasNext pattern behaves in exactly the same way as the invocation hasNext Pattern.
0コメント