Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method named readEntireFile that accepts a Scanner representing an input

ID: 3655011 • Letter: W

Question

Write a method named readEntireFile that accepts a Scanner representing an input file as its parameter, then reads that file and returns the entire text contents of that file as a String. ______________________________________________________________________________________________ This is what I have...There is a file.txt saved in the same directory has the program import java.io.*; import java.util.*; public class ReadEntireFile { public static void main(String[] args) throws FileNotFoundException { String text = readEntireFile(file); } public static String readEntireFile(String file) throws FileNotFoundException{ Scanner input = new Scanner(new File("file.txt")); String text = ""; while (input.hasNextLine()) { text += input.nextLine() + " "; } return text; } }

Explanation / Answer

Introduction In chapter 4 we saw how to construct a Scanner object to read input from the Console. Now we will see how to construct Scanner objects to read input from files. The idea is fairly straightforward, but Java does not make it easy to read from input files. This is unfortunate because many interesting problems can be formulated as file processing tasks. Many introductory computer science classes have abandoned file processing altogether or the topic has moved into the second course because it is considered too advanced for novices. There is nothing intrinsically complex about file processing. The languages C++ and C# provide mechanisms for easily reading and writing files. But Java was not designed for file processing and Sun has not been particularly eager to provide a simple solution. They did, however, introduce the Scanner class as a way to simplify some of the details associated with reading files. The result is that file reading is still awkward in Java, but at least the level of detail is manageable. Before we can write a file processing program, we have to explore some issues related to Java exceptions. Remember that exceptions are errors that halt the execution of a program. In the case of file processing, we might try to open a file that doesn't exist, which would generate an exception. 6.2 Using a Scanner to Read an External File An external file is a collection of characters and numbers that appear on one or more lines. They are external because they are not contained within the program and are not obtained from the user during execution. They are outside the scope of the program and its execution. You create external input files before the execution of a program. For example, you might create a file called "numbers.dat" with the following content. 308.2 14.9 7.4 2.8 3.9 4.7 -15.4 2.8 You can create such a file with an editor like NotePad on Windows or TextEdit on the Macintosh. Then you might write a program that processes this external input file and produces some kind of report. Such a program should be general enough that it could process any external input file that matches a specific format. The Scanner class that we have been using since chapter 4 is flexible in that we can attach Scanner objects to many different kinds of input. You can think of a Scanner object as being like a faucet that you can attach to a pipe that has water flowing through it. For example, in a house we'd attach a faucet to a pipe carrying water from the city or from a well. But we also see faucets in mobile homes and airplanes where the source of water is different. We have been constructing our Scanner objects by passing System.in to the Scanner constructor: Scanner console = new Scanner(System.in); This instructs the computer to construct a Scanner that reads from the console (i.e, pausing for input from the user). Instead of passing "System.in" to the constructor, we can pass information about the external input file to have the input come from the file rather than from the console. In particular, we can construct Scanner objects by passing an object of type File. A File object stores information about where to find an external input file. File objects in turn are constructed by passing a String that represents the file's name. For example, given our file called "numbers.dat", we can construct a File object that is linked to it: new File("numbers.dat") And using this File object we can construct a Scanner object: new Scanner(new File("numbers.dat")) Putting this all together, we'd say something like the following: Scanner input = new Scanner(new File("numbers.dat")); This particular line of code or something like it will appear in all of your file processing programs. Once we've constructed the Scanner so that it reads from the file, we can manipulate it like any other Scanner. When we read from the console, we always prompt before reading to give the user an indication of what kind of data we want. When we read from a file, we don't need to prompt because the data is already there, stored in the file. For example, we might write the following short program to read 5 numbers from the file and to echo the five numbers along with their sum: // Flawed program--doesn't even compile. import java.io.*; import java.util.*; public class Echo1 { public static void main(String[] args) { Scanner input = new Scanner(new File("numbers.dat")); double sum = 0.0; for (int i = 1; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote