Question

So I made this very simple program that places the string "50" after 50 words the app works perfectly. The problem is just here:

if(words == 50){
    text.insert("50", words * charsa);
}

here is the testing code:

package wordscount;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Shady Kamal
 * 
 */

public class WordsCount {

    boolean working;
    int words = 0;
    int wordneed = 50;
    int charsa;
    JTextArea text = new JTextArea(600, 600);
    JFrame frame = new JFrame("Counter for fast reading");
    JButton launch = new JButton("Start");
    String atext;
    boolean isword;

   public WordsCount(){
       frame.setSize(700, 700);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       launch.addActionListener(new ActionListener(){

           @Override
           public void actionPerformed(ActionEvent e) {

               words = 0;
               atext = text.getText();
               charsa = atext.length();

               for (int i = 0; i < charsa; i++) {

                   if ( Character.isLetter(atext.charAt(i)) == false ){
                       isword = false;
                   }else if (i == 0){
                       isword = true;
                   }else if ( Character.isLetter(atext.charAt(i-1)) ){
                       isword = false;
                   }else if ( atext.charAt(i-1) == '\'' && i > 1 && 
                              Character.isLetter(atext.charAt(i-2)) ){
                       isword = false;
                    }else{
                       isword = true;
                    }

                    if (isword){
                       words++;
                       if(words == 50){
                           text.insert("50", words * charsa);
                       }
                    }
                }
                System.out.println("Chars:" + charsa);
                System.out.println("Words:" + words);
            }
        });

        frame.add(text, BorderLayout.CENTER);
        frame.getContentPane().add(new JScrollPane(text),BorderLayout.CENTER);
        frame.add(launch, BorderLayout.SOUTH);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        JOptionPane.showMessageDialog(null, "" + "Instructions: \n" +
            "_________________________________________\n" +
            "Just paste text right here in the TextBox\n" +
            "and click the \"Start\" button when your'e ready\n" +
             "the program will do the rest.\n");
        new WordsCount();
    }
}

and when i run it and write 50 or above words this error is thrown (The Irony in it is that it says at the end "Build Sucessfull", It doesen't seem so successfull to me at all[even that it worked and not failed at building compiling and running] thats still a huge bug to handle from 3 lines of codes!)

run:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid insert
at javax.swing.JTextArea.insert(JTextArea.java:461)
at wordscount.WordsCount$1.actionPerformed(WordsCount.java:66)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 21 seconds)
Was it helpful?

Solution

This is a runtime error, which still can occur even if your code compiles with no errors.

The Javadocs for the insert method state:

Throws:

IllegalArgumentException - if pos is an invalid position in the model

Your position words * charsa must not exist in your JTextArea (yet). Either ensure that the position is already there or modify your formula for the position so it refers to an existing position.

OTHER TIPS

That insert is giving you error, because charsa is the size of your JTextArea, and you are even multiplying it, so it obviously exceeds the JTextArea size.

I guess this is what you want. Give it a try:

    @Override
    public void actionPerformed(ActionEvent e) {

    int wordsSize = 0;
    words = 0;
    atext = text.getText();
    charsa = atext.length();


    for (int i = 0; i < charsa; i++) {

        wordsSize++;

        if(! Character.isLetter(atext.charAt(i)) && Character.isLetter(atext.charAt(i-1)))
        {
            isword = true;
        }

        else
        {
            isword= false;
        }

        if (isword){
            words++;
            if(words == 50){
                text.insert("50", wordsSize);
            }
        }
    }
    System.out.println("Chars:" + charsa);
    System.out.println("Words:" + words);

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top