

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Reading from files When you have a lot of data to process, it is unreasonable to read it from the keyboard. Instead we read data from a file or a database. In this course, we will read the data from a text file, that is a file consisting of ASCII characters. It is easiest to construct such a file using Notepad. The first thing we have to do when reading from a file is to declare and open the file. This is done with Scanner. The file must be saved in the project folder, and if Scanner cannot find it, there will be an error. Errors in Java are called exceptions. The exception caused by not finding the file is the FileNotFoundException. Scanner will try to find the file, and if it succeeds it will do so. If not Java catches the exception and executes the code in after the catch clause. Scanner fileScan; try { fileScan = new Scanner (new File ("expenses.txt")); } catch (FileNotFoundException e) {System. out .println ("File not found");} It is convenient to place this code in the constructor, so that it will be performed as soon as the class is instantiated. When processing the file, the program has to read every character in it. Some of the characters are not visible in the file, but they have to be there. Spaces are easily seen if they are in the middle of the line, but they can be missed if they are at the end. And the enter key always attaches and end of line character after each line. It must also be read. Full lines are read using item = fileScan.nextLine (). This method will read all characters from the first non-space character to the end of the line and store the result in the String variable, item. It also reads the end of line character and discards it. Numbers are read using nextInt () and nextDouble (). They do not read the end of line character, so it must be read separately. This is because the file may have several numbers on the same line as in the following example. panda 6 80.0 1.6 Mei Ling This file starts with a String, the type of the animal. It is followed by a line with three numbers and a name. This was done so that when reading the name, the end of line character would also be read. String type = fileScan.nextLine (); int age = fileScan.nextInt (); double weight = fileScan.nextDouble (); double height = fileScan.nextDouble (); String name = fileScan.nextLine (); When reading numbers, Scanner skips all the leading spaces and tabs. It then reads the characters (digits) in the number and converts them either to an integer or a double. This file was set up on purpose so that each line ended with a String, so that when nextLine () is used, it will get rid of the end of line character.
If a line ends with a number, the end of line character must be read separately. This can be done by creating a String variable (which will end up being empty) to hold it. panda 6 80.0 1. Mei Ling This can be read by String type = fileScan.nextLine (); int age = fileScan.nextInt (); double weight = fileScan.nextDouble (); double height = fileScan.nextDouble (); String eol = fileScan.nextLine (); String name = fileScan.nextLine (); It is very important to read the data in the same order as it is listed in the file, including end of line characters. In fact, if we leave out the line String eol = fileScan.nextLine (); we will generate a InputMismatchException. This exception is thrown when the input is of a different type then that being read. Since a file usually consists of a number of lines, they are read using a while loop. If the file is panda 6 80.0 1.6 Mei Ling meerkat 3 0.9 0.3 Kenzu we can read it with while (fileScan.hasNext ()) { String type = fileScan.nextLine (); int age = fileScan.nextInt (); String name = fileScan.nextLine (); // Process the data next. }