`CountSpace1' example program: demonstrates string-array interaction

CountSpace1.java

// CountSpace1.java
// A program that counts the spaces in a text string 
// Kevin Boone, August 1999

import java.applet.Applet;
import java.awt.*;

public class CountSpace1 extends Applet
{
public void paint (Graphics g)
	{
	int numberOfSpaces = 0;
	String text = "The cat sat on the mat";
	for (int i = 0; i < text.length(); i++)
		if (text.charAt(i) == ' ')
			numberOfSpaces++;
	
	g.drawString ("There are " + Integer.toString(numberOfSpaces) + " spaces", 20, 40);
	}
}


©1994-2003 Kevin Boone, all rights reserved