您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Extend Index
Extend Page # 11

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
basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg