Reading Data from a File Practice

posted by: Mr. Bergquist 4 December 2013 No Comment

For reference here are the presentations on File input:
File input with Scanner
Line-based file input
Handout: File Input with Scanners
Starter.java - template to help solve these problems & use at the Programming Contest

To start out, in class we will solve the first problem (2.1) of the April 2009 contest which requires the data file: pr21.dat  – here is the simple solution:  Pr2D1.java   (Later, after the Line-based file input presentation, we review this more complicated Java solution Pr2dot1.java to set up a more robust input file for the programming contest.)

Then do these 6 Problems: YOU CAN DO THESE IN A GROUP OF UP TO 3 STUDENTS!

1) & 2) Do Problems 3 – Groceries  &  4 – Triangles  of the 2011 Novice Programming Contest (pdf)  Data files:  grocery.dat  and triangles.dat  The rest of the problems data files are zipped up here: ( Dec2011Novice.zip / 2011 Novice Programming Contest)

3) & 4) Do Problems 2.2 OUNCES TO MILLILITERS  &  2.3 FAT FOOD of the April 2009 contest   Data files: pr22.dat  and pr23.dat  The rest of data problems files are zipped up here: Data files (Extra data files)

And a couple taken directly from Practice It!:  (click the problem name link or try the problem directly in case it is down.)

5) Fix:  BJP3 Self-Check 6.11: Oops6-errors
The following program contains 6 errors. Correct the errors and submit a working version of the program. The corrected version of the program should produce good output like:

Lines: 5
Words: 21

public class Oops6 {
    public static void main(String[] args) {
        Scanner in = new Scanner("example.txt");
        countWords(in);
    }

    // Counts total lines and words in the input scanner.
    public static void countWords(Scanner input) {
        Scanner input = new Scanner("example.txt");
        int lineCount = 0;
        int wordCount = 0;

        while (input.nextLine()) {
            String line = input.line();   // read one line
            lineCount++;
            while (line.next()) {         // tokens in line
                String word = line.hasNext;
                wordCount++;
            }
        }

        System.out.println("Lines: " + lineCount);
        System.out.println("Words: " + wordCount);
    }
}
6) BJP3 Exercise 6.5: 

Write a static method named collapseSpaces that accepts a Scanner representing a file as a parameter and writes that file’s text to the console, with multiple spaces or tabs reduced to single spaces between words that appear on the same line. For example, if a Scannervariable named input is reading an input file containing the following text:

four      score   and
seven               years ago         our

fathers brought             forth
    on this          continent
a         new

nation

then the call collapseSpaces(input); should produce the following output:

four score and

seven years ago our

fathers brought forth
on this continent
a new

nation

Each word is to appear on the same line in output as it appears in the file. Notice that lines can be blank.

When you are done do more CONTEST PROBLEM PRACTICE…

These problems are a great way to bring together all that we’ve learned from expressions all the way to file input. Plus, they’re pretty satisfying — I think you’ll find them very approachable.

Previous Year’s Problem Sets:

  • 2013 Novice Programming Contest with data files zipped up here: StudentDataDec2013.zip
  • 2011 Novice Programming Contest  with data files zipped up here: Dec2011Novice.zip
  • April 2009 contest – the problem number indicates how many points it is worth. For example, problem 2.1 is worth two points while 9.1 is worth 9 points. Naturally, the higher the point value, the harder the problem! I recommend 5.1, 5.2, 5.3, 5.4, 5.6, 9.3 and 9.6 to practice skills we’ve specifically worked on.Data filesExtra data files.
  • Fall 2008 contest – the problem number indicates how many points it is worth. For example, problem 2.1 is worth two points while 9.1 is worth 9 points. Naturally, the higher the point value, the harder the problem! I recommend 2.5, 2.6, 5.1, 5.4, 5.7, 9.1 and 9.8
  • Spring 2010 contest / Judges data files (sorry I don’t have the student data files)
  • Advanced problems – the competition has two divisions: advanced and novice. Advanced is for students who have completed a programming course. Check these out, though, because there are many you can do now…
About: Practice-It! web application and problems are copyright © Marty Stepp unless otherwise specified. Some problems are copyright © Pearson. This site is not directly affiliated or associated with Pearson or with any particular textbook or online resource. Any non-educational usage of the content on this site is expressly forbidden without written permission. All rights reserved.
1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 3.88 out of 5)
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>