Processing An Unknown Number Of Parameters

import java.applet.*;    
import java.awt.*; 
             
public class PoetryApplet extends Applet {

  private String[] poem = new String[101];
  private int numLines;

  public void init() {

    String nextline;

    for (numLines = 1; numLines < poem.length; numLines++) {
      nextline = this.getParameter("Line" + numLines);
      if (nextline == null) break;
      poem[numLines] = nextline;
    }
    numLines--;
    
  }
  
  public void paint(Graphics g) {
  
    int y = 15;
  
    for (int i=1; i <= numLines; i++) {
      g.drawString(poem[i], 5, y);    
      y += 15;
    }
    
  }
  
} 

Here's the applet:

There once was a man from Japan Whose poetry never would scan When asked reasons why, He replied, with a sigh: I always try to get as many syllables into the last line as I can.

You might think it would be useful to be able to process an arbitrary list of parameters without knowing their names in advance, if nothing else so you could return an error message to the page designer. Unfortunately there's still no way to do it in Java 1.5. (I asked James Gosling for this way back Java 1.0a3 in 1995, and he thought it would be a good idea.) It may appear in future versions.


Previous | Next | Top | Cafe au Lait

Copyright 1997-1999, 2002, 2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified July 1, 2005