home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap30 / ExceptionApplet5.java < prev    next >
Encoding:
Java Source  |  1996-03-20  |  1.3 KB  |  55 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class ExceptionApplet5 extends Applet
  5. {
  6.     TextField textField1, textField2;
  7.     String displayStr;
  8.     int array[];
  9.  
  10.     public void init()
  11.     {
  12.         textField1 = new TextField(15);
  13.         add(textField1);
  14.         textField2 = new TextField(15);
  15.         add(textField2);
  16.         displayStr = "";
  17.         array = new int[100];
  18.     }
  19.  
  20.     public void paint(Graphics g)
  21.     {
  22.         Font font = new Font("TimesRoman", Font.PLAIN, 18);
  23.         g.setFont(font);
  24.  
  25.         g.drawString("Valid array indexes are 0 to 99", 15, 60);
  26.         g.drawString(displayStr, 70, 130);
  27.     }
  28.  
  29.     public boolean action(Event evt, Object arg)
  30.     {
  31.         String str1 = textField1.getText();
  32.         String str2 = textField2.getText();
  33.  
  34.         try
  35.         {
  36.             int index = Integer.parseInt(str1);
  37.             int value = Integer.parseInt(str2);
  38.             array[index] = value;
  39.             displayStr = "Value stored.";
  40.         }
  41.         catch (ArrayIndexOutOfBoundsException e)
  42.         {
  43.             displayStr = "Out of bounds";
  44.         }
  45.         catch (NumberFormatException e)
  46.         {
  47.             displayStr = "Bad format";
  48.         }
  49.  
  50.         repaint();
  51.         return true;
  52.     }
  53. }
  54.  
  55.