Solved

Simple Applet Problem

Posted on 2003-11-10
6
233 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now