How to Take Input in Java Using a Scanner
Using the Scanner class in Java, you can read data from external sources such as text files. The process only requires a few straightforward steps, but you do need to tailor it to suit your own file and program. The Java platform provides standard libraries you can use for input and output operations. By first creating instances of the classes in these libraries and then using methods of the Scanner class to read your file content, you can acquire the content in a way that suits the logic of your program.
Instructions
-
-
1
Import the necessary Java resources for your input process. Add the following statement at the top of your Java class file, importing the standard Java resources for input and output:
import java.io.*;
In order to use the Scanner class, you also need to import the class file for it, so add the following additional import statement:
import java.util.Scanner;
Once your program has these classes imported, you can create objects to carry out input operations as you require.
-
2
Create try and catch blocks to take care of any input exceptions that may occur. When your Java programs read data from an external source, you risk unforeseen errors, such as a file not being where it should be or not having the correct content in it. For this reason, you need to include your input processing code inside a try block, following this with a catch block to handle exceptions, as follows:
try {
//try to carry out input processes here
}
catch(IOException ioException) { System.out.println(ioException.getMessage()); }If the program does throw an exception, your code will write the details out to standard output.
-
-
3
Instantiate the input and Scanner classes for your operation. To use a Scanner object, you first need to create FileReader and BufferedReader objects. Add the following code inside your try block, creating an instance of the FileReader class and passing it the name and location of your own file as a parameter:
FileReader fileRead = new FileReader("yourfile.txt");
Add the following line, creating an instance of the BufferedReader class, passing your FileReader instance as a parameter:
BufferedReader buffRead = new BufferedReader(fileRead);
Create an instance of the Scanner class, passing it your BufferedReader object, as follows:
Scanner fileScan = new Scanner(buffRead);
Your program is now ready to read and process the content of the file.
-
4
Use a while loop to process your file content. The Scanner can read your file in sections, so you need it to continue reading until the file has been exhausted. Add the following loop outline structure inside your try block, on the line after you create your Scanner object:
while(fileScan.hasNext()) {
//read the file contents here
}
//close the scanner
fileScan.close();This loop will keep executing until the Scanner has read all of the file contents. Inside the loop, you can add processing to scan each item of data in the file. Once your loop finishes, the Scanner has done its job, so you can close it.
-
5
Read the content of your file using the Scanner. The Scanner class gives you a range of options in terms of how you process the content of your file. You can read the file one line at a time, or can read single bytes and numbers, with various numerical types supported. To read the file in individual lines, add the following code inside the while loop:
String nextLine = fileScan.nextLine();This code stores a single line from the file in a String variable each time the loop executes. You can carry out whatever tasks you need using the file content inside the while loop. (See References 1, 2)
-
1
Tips & Warnings
Try the different Scanner methods to read your file content until you find one to suit your program.
Programs using external data generally need lots of testing to ensure they function correctly.
References
Resources
- Photo Credit Ryan McVay/Digital Vision/Getty Images