Assignment 2: Problem Solving Example


First, the easy parts

 

Important: We will reveal the full solution here, more or less. However, in each step before we reveal the next step, we're going to ask you to give it a try. You will learn

Let's start with some ideas:

 

Let's flesh this out with a little code:

 

A2.2 Exercise: Before you read further, try to puzzle out what else needs to be in getChar().
 

There is another issue to worry about:

 

Now, let's put the above two pieces together:


public class ReadFileByChars {

    static Scanner fileScanner;
    static String line;

    static void openFile (String filename)
    {
	try {
            // Make a file scanner as in Module 7.
            fileScanner = new Scanner (new File (filename));
	}
	catch (IOException e) {
	    System.out.println ("Could not open file: " + filename);
	    System.exit (0);
	}
    }

    static char getNextChar ()
    {
        // What do we do with this? And when do we return a char?
        line = fileScanner.nextLine ();

	// This won't work:
        char c = line.charAt(0);
	return c;
    }

}
    
 

We need to keep track of where we are in the line: another global variable is needed:

 

A2.3 Exercise: Examine the code in getChar() and answer these questions:

  1. How do we know if we've reached the end of the line?
  2. Suppose we've reached the end of the line. Then, inside the if-block, we're reading the next line from the file. How do we know there are more lines to read? And if there are more lines, when and where do we read them?
  3. What would happen if, instead of at the end of getChar() the return statement occured in the second line:
        static char getNextChar ()
        {
            char c = line.charAt (currentPositionInLine);
    	return c;
            currentPositionInLine ++;
            if ( ... reached end of line ...) {
                // Get the next line.
                line = fileScanner.nextLine ();
            }
        }
        
 

We're now in a position to address these issues:


public class ReadFileByChars {

    static Scanner fileScanner;
    static String line;
    static int currentPositionInLine = 0;

    static void openFile (String filename)
    {
	try {
            // Make a file scanner as in Module 7.
            fileScanner = new Scanner (new File (filename));
	}
	catch (IOException e) {
	    System.out.println ("Could not open file: " + filename);
	    System.exit (0);
	}
    }

    static char getNextChar ()
    {
        char c = line.charAt (currentPositionInLine);
        currentPositionInLine ++;
        if ( currentPositionInLine >= line.length() )
            // Get the next line if there are any.
            if ( fileScanner.hasNextLine() ) {
                line = fileScanner.nextLine ();
            }
            else {
                // Nothing further so return '#', our special END marker:
                return '#';
            }
        }
	return c;
    }
    
 

A2.4 Exercise: There are two problems with the code above. Before reading further, can you look over and try to find them? We're not going to address these here, but will do later.
 


The harder parts

 

One problem with the above approach:

 

We will exploit the fact that all chars are int's but not vice-versa:

 

Here's the program:

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

public class ReadFileByChar {

    static Scanner fileScanner = null;
    static String line = null;
    static int currentPositionInLine = 0;

    public static void main (String[] argv)
    {
	// Set up the scanner for this file and get the first line:
	openFile ("somefile.txt");

	// Loop through one char at a time
	int i = getNextChar ();
	while (i >= 0) {
	    char c = (char) i;
	    System.out.print (c);
	    i = getNextChar ();
	}
	System.out.println ();
    }

    static void openFile (String filename)
    {
	try {
	    fileScanner = new Scanner (new File (filename));

	    // Start by getting the first line:
	    if (fileScanner.hasNextLine()) {
		line = fileScanner.nextLine ();
	    }
	}
	catch (IOException e) {
	    System.out.println ("Could not open file: " + filename);
	    System.exit (0);
	}
    }

    static int getNextChar ()
    {
	// If we're already at the end, look for the next line.
	if (currentPositionInLine >= line.length()) {
	    if (fileScanner.hasNextLine()) {
		line = fileScanner.nextLine ();
		currentPositionInLine = 0;
	    }
	    else {
		// If no more lines, we're done.
		return -1;
	    }
	}

	char c = line.charAt (currentPositionInLine);
	currentPositionInLine ++;
	return (int) c;
    }

}
    
 

A2.5 Exercise: There is one bug in the above program. Write it up, and as you do so, add proactive println's to identify and fix the bug.

Back to the assignment



© 2017, Rahul Simha