Listing of CountSpace1.java / / CountSpace1.java // A program that counts the spaces in a text string // Kevin Boone, August 1999 // modified Matt Smith, Sep 1999 // <APPLET code="CountSpace1.class" width=275 height=200></APPLET> 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"; g.drawString ("Counting spaces in '" + text + "'", 20, 20); for (int i = 0; i < text.length(); i++) if (text.charAt(i) == ' ') numberOfSpaces++; g.drawString ("There are " + Integer.toString(numberOfSpaces) + "spaces", 20, 40); } } Back to top
 RITSEC - Global Campus Copyright © 1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg
|