Solved

Simple Applet Problem

Posted on 2003-11-10
6
235 Views
Last Modified: 2010-03-31
Hello Experts;

I have a simple problem. I wrote an applet program which tries to parse a set of
array words. If found it will display an error screen but when I tried to run it, it
gives me a set of ArrayIndexOutOfBoundsException: 5 errors.

Here is the codes :

b1.addActionListener(new ActionListener()  {
 
        public void actionPerformed(ActionEvent h)
      {  
            String x[] = {"Word","Word","Word","Word","Word"};
            int i;
            
            for (i=0; i<=x.length; i++ ) {
            if (t1.equals(""+x[i])) {      
            JOptionPane.showMessageDialog(null,"Your words are not allowed !",
            "Sorry . . .",JOptionPane.WARNING_MESSAGE);
            }
            }
      }
      });

Can anyone fix this for me, thanks.
simple 21
0
Comment
Question by:simple21
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9714880
>>for (i=0; i<=x.length; i++ ) {


should be

for (i=0; i<x.length; i++ ) {
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9714889
Word=0
Word=1
Word=2
Word=3
Word=4

length is equal to 5 (there is no element with index 5)
0
 

Author Comment

by:simple21
ID: 9714982
>> for (i=0; i<x.length; i++ ) {
     
Tried it but did not worked.

Here's my code :

import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class JGuestBook extends JApplet {
      
      private JTextField t1;
      private JTextArea t2;

      public void init () {
      
      JDesktopPane x1 = new JDesktopPane();
      t1 = new JTextField("1.) Enter name here . . .");
      t2 = new JTextArea("2.) Enter comments here . . .");
      
      JButton b1 = new JButton(" Sign My Guestbook ");
      JButton b2 = new JButton(" Clear ");
      JPanel p1 = new JPanel();
      
      p1.add(b1); p1.add(b2);
      
      JInternalFrame f1 = new JInternalFrame("JGuestBook v 1.0 . . .",false,false,true,true);

      Container c = f1.getContentPane();
      c.add( new JScrollPane(t2) );
      c.add(t1, BorderLayout.NORTH);
      c.add(p1, BorderLayout.SOUTH);
      f1.setSize(300, 250);
      f1.setVisible(true);
      x1.add(f1);
      
      this.getContentPane().add(x1, BorderLayout.CENTER);
      
      b1.addActionListener(new ActionListener()  {
 
        public void actionPerformed(ActionEvent h)
      {  
            String x[] = {"Word","Word","Word","Word","Word"};
            int i;
            
            for (i=0; i<x.length; i++ ) {
            if (t1.equals(""+x[i])) {      
            JOptionPane.showMessageDialog(null,"Bad words not allowed !",
            "Sorry . . .",JOptionPane.WARNING_MESSAGE);
            }
            }
      }
      });
      
      b2.addActionListener(new ActionListener()  {
 
        public void actionPerformed(ActionEvent h)
      {  
            t1.setText("");
            t2.setText("");
      }
      });
      }
}
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9715029
The reason for the index out of bounds was as i gave.

>>Tried it but did not worked.

What do you mean by that?
0
 

Author Comment

by:simple21
ID: 9715070
Lets say :

String x[] = {"Word1","Word2","Wor3","Wor4","Wor5"};

For example I put a "Word1" on my JTextfield and pressed my
b1 button it should display a JOption Warning Message
but it does not show the warning.

Id this wrong ?

 if (t1.equals(""+x[i])) {    
          JOptionPane.showMessageDialog(null,"Bad words not allowed !",
          "Sorry . . .",JOptionPane.WARNING_MESSAGE);
          }
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 9715283
>> if (t1.equals(""+x[i])) {    

should be

 if (t1.getText().equals(x[i])) {    

t1 is a JTextField - it can't be equal to a String. There is no need to add the String in the array to an empty String
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 62
diffSum example 4 50
how to add new optional parameter to JSP 1 49
CSV file parsing thru Java 13 34
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question