Java Reference
In-Depth Information
Display 10.6 Methods in the Class Scanner (part 1 of 4)
Scanner is in the java.util package.
public Scanner(InputStream streamObject)
There is no constructor that accepts a file name as an argument. If you want to create a stream using
a file name, you can use
new Scanner( new FileInputStream( File_Name ))
When used in this way, the FileInputStream constructor, and thus the Scanner constructor invo-
cation, can throw a FileNotFoundException , which is a kind of IOException .
To create a stream connected to the keyboard, use
new Scanner(System.in)
public Scanner(File fileObject)
The File class will be covered in the section entitled “The File Class,” later in this chapter. We dis-
cuss it here so that you will have a more complete reference in this display, but you can ignore this
entry until after you've read that section.
If you want to create a stream using a file name, you can use
new Scanner( new File( File_Name ))
public int nextInt()
Returns the next token as an int , provided the next token is a well-formed string representation of
an int .
Throws a NoSuchElementException if there are no more tokens.
Throws an InputMismatchException if the next token is not a well-formed string representation of
an int .
Throws an IllegalStateException if the Scanner stream is closed.
public boolean hasNextInt()
Returns true if the next token is a well-formed string representation of an int ; otherwise returns
false .
Throws an IllegalStateException if the Scanner stream is closed.
public long nextLong()
Returns the next token as a long , provided the next token is a well-formed string representation of a
long .
Throws a NoSuchElementException if there are no more tokens.
Throws an InputMismatchException if the next token is not a well-formed string representation of
a long .
Throws an IllegalStateException if the Scanner stream is closed.
Search WWH ::




Custom Search