Scanner
The Scanner
class is part of the java.util
package and primarily used to read in inputs from a variety of different sources, such as input streams, users, files, etc.
Method Details
Scanner
public Scanner(System.in)
Creates a Scanner
object to read input entered by a user in the console.
Parameters
Name | Type | Description |
---|---|---|
System.in | InputStream | take inputs from the standard input (i.e., the keyboard) |
Examples
Scanner input = new Scanner(System.in);
Scanner
public Scanner(File source)
Creates a Scanner
object to read the contents of the specified File
object.
Parameters
Name | Type | Description |
---|---|---|
source | File | Input is of the type |
Examples
Scanner fileReader = new Scanner(File source);
close
public void close()
This method closes a Scanner
object that has been opened.
Examples
Scanner input = new Scanner(System.in);
System.out.println("Say something: " + input.nextLine());
input.close(); // close the Scanner object
hasNext
public boolean hasNext()
The hasNext()
method returns true
if the Scanner
has another token in its input.
Examples
File myFile = new File("samplefile.txt");
// create Scanner object with the file
Scanner fileReader = new Scanner(myFile);
// check if the Scanner object has a token to read
System.out.println("Result: " + fileReader.hasNext());
// print the contents
System.out.println("File contents: " + fileReader.next());
fileReader.close();
Output:
Result: true
File contents: Hello
hasNextBoolean
public boolean hasNextBoolean()
The hasNextBoolean()
method returns true
if the next token in the Scanner
object is a boolean
.
Examples
File myFile = new File("samplefile.txt");
// create a new Scanner object to read the file
Scanner fileReader = new Scanner(myFile);
// check if the Scanner's next token is a boolean
System.out.print(fileReader.hasNextBoolean());
System.out.print(" -> " + fileReader.nextBoolean() + "\n");
fileReader.close();
Output:
true -> false
hasNextDouble
public boolean hasNextDouble()
The hasNextDouble()
method is used to determine if the next token in the Scanner
object's input can be interpreted as a double
. It will return true
if the Scanner
object's input can be interpreted as a double
, otherwise returns false
.
Examples
if (input.hasNextDouble()) {
System.out.println("Identified a double" + input.nextDouble());
}
input.close();
hasNextInt
public boolean hasNextInt()
The hasNextInt()
method is used to determine if the next token in the Scanner
object's input can be interpreted as an int
. It will return true
if the Scanner
object's input can be interpreted as an int
, otherwise returns false
.
Examples
if (hasNextInt()) {
System.out.println("Identified an int object" + input.nextInt());
}
input.close();
hasNextLine
public boolean hasNextLine()
The hasNextLine()
method returns true
if the Scanner
input object has another line of input.
Examples
File myFile = new File("samplefile.txt");
Scanner fileReader = new Scanner(myFile);
while (fileReader.hasNextLine()) {
System.out.println(fileReader.nextLine());
}
fileReader.close();
next
public String next()
The next()
method finds the next token from the Scanner
input object and returns it.
Examples
Scanner input = new Scanner(System.in);
System.out.print("Enter your first name: ");
// read in the input
String firstName = input.next();
// print the value read by the Scanner object
System.out.println("First name is: "+ firstName);
input.close();
nextBoolean
public boolean nextBoolean()
The nextBoolean()
method scans the input as a boolean
value and then returns that value. If the next value in the input cannot be translated into a valid boolean
value, it will throw an InputMismatchException
.
Examples
if (input.hasNextBoolean()) {
System.out.println("Found a boolean :" + input.nextBoolean());
}
input.close();
nextDouble
public double nextDouble()
The nextDouble()
method reads the input as a double
value and then returns that value. If the next value in the input cannot be translated into a valid double
value, it will throw an InputMismatchException
.
Examples
if (input.hasNextDouble()) {
System.out.println("Found double value :" + input.nextDouble());
}
input.close();
nextInt
public int nextInt()
The nextInt()
method reads the input as an int
value and then returns that value. If the next value in the input cannot be translated into a valid int
value, it will throw an InputMismatchException
.
Examples
if (input.hasNextInt()) {
System.out.println("Found int value: " + input.nextInt());
}
input.close();
nextLine
public String nextLine()
The nextLine()
method reads the contents of the user input or a file until a new line is encountered.
Examples
File myFile = new File("samplefile.txt");
Scanner fileReader = new Scanner(myFile);
System.out.println(fileReader.nextLine());
fileReader.close();