Solved

Simple Applet Problem

Posted on 2003-11-10
6
234 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Filesystem manipulation of Word Docx file (find replace text) 3 64
stackato and cloud 4 64
Java Options in Zimbra Server 2 44
fibonacci ten numbers 4 23
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

929 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

13 Experts available now in Live!

Get 1:1 Help Now