File Reading Crash Course.

posted by: Ms. Martin 28 November 2009 One Comment

On our short day on Wednesday, I showed some parts of a TED talk by Hans Rosling, founder of Gapminder.org, a tool for visualizing statistics.  Hopefully you were struck by the power of software to make sense of otherwise difficult to interpret data.  We’ll be looking at reading and visualizing data from files for a bit.

As a first foray into the world of processing data from an existing source, we’ll work on some programming competition practice problems.  All of these require you to read information in from a file, do some data-crunching and create output.

Reading input from files is very similar to reading input from a user: we’ll also use Scanner objects.  Recall that you can think of Scanner as a faucet that needs a source of tokens.  For file reading, instead of getting input from System.in as we did for user input, we’ll have to create File objects.  File is a class in the java.io package, so every program that reads user input will need the following line at its top:

import java.io.*;

Creating a file object from a filename is done as follows:

File f = new File("filename"); // looks for a file called filename in the current directory

Once we have a file object, we can create a Scanner on it.  There is a possibility for error, here, though — what if the filename is invalid or doesn’t refer to a real file?  Java would crash by doing something called throwing an exception.  Thus, whenever we use a file object, we have to provide Java with some way of preventing the program from outright crashing.  Exception handling is done by using the try/catch construct as follows:

try {

    Scanner s = new Scanner(f);
    // all your code using file stuff goes here

} catch (FileNotFoundException e) {
    System.out.println(e);
}

If we don’t know how much data there will be in the file ahead of time, then we need some way of telling Java to go until the end of the file is reached.  We have to use a while loop and a test on the Scanner to see if there are input lines remaining.

while(s.hasNextLine()) {
    String line = s.nextLine();
}

We don’t have to do that with the programming competition examples, though, since the first line always gives the size of the data set.  We can therefore use a for loop.  See CompetitionPractice.java for a commented example.

In order to handle file-based data, we often have to interpret a line of data as multiple related pieces of information.  For example, if we have a data file of hours employees have worked, each line may look like the following:

Jenny 10 8 7 8 9

We’d like to interpret that as a name and hours worked for each day of the week, but a call on nextLine will just give us one big string.  That’s where using a Scanner on a String can come in handy.  

String line = "Jenny 10 8 7 8 9";
Scanner lineScan = new Scanner(line);
String name = lineScan.next();  // First token on line is a name
while(lineScan.hasNextInt()) { // While there are still numbers to process
    int hoursWorked = lineScan.nextInt(); // Grab a number
    System.out.println(hoursWorked);
}

Try copying and running the pieces of example I’ve given in this post.  It will probably help to look at the fully commented example to see how pieces fit together.  Next, try your hand at some of the simple 2 or 3 point questions.  Have fun!!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>