Solved

Simple Applet Problem

Posted on 2003-11-10
6
238 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
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!

 
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

Independent Software Vendors: 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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

631 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