Solved

Simple Applet Problem

Posted on 2003-11-10
6
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 83
hibernate insert example 13 38
Eclipse with various Java releases 7 47
DTD and JAVA versions 1 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 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