Compsci 100e, Spring 2011,
Simple Word Count Exercises

Names and NetID of students in group (min 2, max 3)
Name: __________________________   NetID: _____________

Name: __________________________   NetID: _____________

Name: __________________________   NetID: _____________

SimpleWordCount

Questions about the code follow the code listing. The source for the code is in SimpleWordCount.java.

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

public class SimpleWordCount {
    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner in = new Scanner(new File("test.txt"));
	in.useDelimiter("\\Z");
        String text = in.next();
        String[] words = text.split("\\s+");
	System.out.println("Total # words = " + words.length);

        for (int k = 0; k < words.length; k = k + 1) {
            System.out.println(words[k]);
        }
            
    }
}
Below is the result of running the code with test.txt.

Total # words = 10 Words are great words are fun words are for everyone Review the code above and focus on the main method. To the best your ability, describe what you think each line of code does when executed. What data is involved? How are is the data being manipulated? What computations or functions are executed?
  1.         Scanner in = new Scanner(new File("test.txt"));
    
    
    
    
  2. 	in.useDelimiter("\\Z");
    
    
    
    
  3.         String text = in.next();
    
    
    
    
  4.         String[] words = text.split("\\s+");
    
    
    
    
  5. 	System.out.println("Total # words = " + words.length);