Little details we take for granted

I'm getting ready for my AP classes this morning. We're building a word search generator and we're at the point where we need to read a list of words from a file

First, I'd better make sure I can do it. We're using the java scanner, mostly because it's easy.

First cut:

public class wl {
    public static void main(String[] args) {
        Scanner sc = new Scanner(new File("words"));
        while (sc.hasNext()){
            String s = sc.next();
            System.out.println(s);
        }
    }
}

Oh yes, I forgot the

import java.io.*;
import java.util.*;

Oh, and also the fact that I've got deal with exceptions when working with files:

try {
    Scanner sc = new Scanner(new File("words"));
    while (sc.hasNext()){
        String s = sc.next();
        System.out.println(s);
    }
} catch (Exception e){
    System.out.println("Can't open file.");
    System.exit(0);
}

But we don't want to wrap our entire program in an exception:

try {
    Scanner sc = new Scanner(new File("words"));
} catch (Exception e){
    System.out.println("Can't open file.");
    System.exit(0);
}

while (sc.hasNext()){
    String s = sc.next();
    System.out.println(s);

}

Now it doesn't work because the Scanner sc might not exist after the try catch block (if an exception occurred), so we need:

Scanner sc
try {
    sc = new Scanner(new File("words"));
} catch (Exception e){
    System.out.println("Can't open file.");
    System.exit(0);
}

while (sc.hasNext()){
    String s = sc.next();
    System.out.println(s);

}

And now this doesn't work because sc might not have a value so we finally get to the working version:

import java.io.*;
import java.util.*;
public class wl {
    public static void main(String[] args) {
        Scanner sc = null;
        try {
                sc = new Scanner(new File("words"));
        } catch (Exception e){
            System.out.println("Can't open file.");
            System.exit(0);
        }
        while (sc.hasNext()){
            String s = sc.next();
            System.out.println(s);

        }
    }
}

Really simple program but that's a long list of things that can go wrong along the way. Nothing big, but each a stumbling block for a beginning student that doesn't have the wealth of experience that someone like me has.

I had an interesting discussion with one of my seniors the other day on this subject - all the base knowledge that experienced programmers have that students don't and how we have to approach teaching so that students are supported and not frustrated by these little but important speed-bumps that they'll hit along the way.

I hope to write more about this soon, but for now I'll leave you to ponder the issue.

Comments

Comments powered by Disqus



Enter your email address:

Delivered by FeedBurner

Google Analytics Alternative