Computer Science 120
Introduction to Programming
Spring 2012, Siena College
CountWords Demo
A working demo of CountWords will appear below. Click inside the applet to interact with it.
CountWords BlueJ Project
Click here to download a BlueJ project for CountWords.
CountWords Source Code
The Java source code for CountWords is below. Click on a file name to download it.
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /* $Id: CountWords.java 1609 2011-04-23 03:04:27Z terescoj $ */ /* * Example CountWords * * @author Jim Teresco, Siena College, CSIS 120, Spring 2011 * */ public class CountWords { private static final String FILENAME = "whosonfirst.txt"; public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner (new File(FILENAME)); int count=0; while (input.hasNext()) { input.next(); // we read the word but just throw it away count++; } System.out.println("Total words = " + count); } }