Taha Rushain
Taha Rushain

Reputation: 635

Index-out-of-bound exception in java

Exception messages:

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2

Following is the code I am using to highlight specific words in JTextPane. My objective is to create a simple syntax highlighting editor, I have searched thoroughly about it and have found many interesting solutions but I wanted to write my own code for it and now I am stuck on an IndexOutOfBoundsException.

My editor gives this exception whenever third key is pressed , meaning whenever 2 letters are written in the JTextPane.

My apologies if the code is not easily understandable, I am new at learning conventions.

I know this is a very trivial question but any sort of help would be considerable. Thank you :)

[Update] The first part of the code works on the jTextPane2KeyTyped event

    String[] words = new String[] {"if","else","for"};
    //words is the list for words to change color

    StyledDocument doc = jTextPane2.getStyledDocument();

    Style style=doc.addStyle("Red_Colour", null);
    StyleConstants.setForeground(style, Color.RED);
    StyleConstants.setForeground(common,Color.BLACK);

    String temp = jTextPane2.getText();
    //temp holds the string value of the text present in the jTextPane2

    int check=0;

    for(int i=0;i<temp.length();i++){
        for(int j=0;j<words.length;j++){
            if(charLeft(temp,words,i,j)){
                if(temp.length()>=words[j].length())
                    for(int k=0;k<words[j].length();k++){
                        if(temp.charAt(i+k)==words[j].charAt(k))check++;
                    }
                    //else{break;}
                    if(check==words[j].length()){
                        doc.setCharacterAttributes(i,words[j].length(),style, false);
                    }
            }
        }
    }

Following is the code for the method called(i.e. charLeft())

   public Boolean charLeft(String temp,String[] words,int i,int j){

    temp= temp.substring(i, temp.length());
    if(temp.length()<words[j].length())return true;
    else return false;
} 

TrackBack for the exceptions

    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(String.java:658)
at NewJFrame.jTextPane2KeyTyped(NewJFrame.java:164)
at NewJFrame.access$000(NewJFrame.java:24)
at NewJFrame$1.keyTyped(NewJFrame.java:59)
at java.awt.Component.processKeyEvent(Component.java:6460)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2828)
at java.awt.Component.processEvent(Component.java:6282)
    ...

    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(String.java:658)
at NewJFrame.jTextPane2KeyTyped(NewJFrame.java:164)
at NewJFrame.access$000(NewJFrame.java:24)
at NewJFrame$1.keyTyped(NewJFrame.java:59)
at java.awt.Component.processKeyEvent(Component.java:6460)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2828)
    ...

Upvotes: 0

Views: 1513

Answers (3)

Taha Rushain
Taha Rushain

Reputation: 635

Fixed! The problem was that I was not changing the value of count back to zero. Other problem was in the conditional operator in charLeft() , which was

if(temp.length()<words[j].length())return true;

Corrected condition is

if(temp.length()>=words[j].length())return true;

Upvotes: 0

BitNinja
BitNinja

Reputation: 1487

I think that this line might be your problem (This is line 3 of the second code block you posted):

    if(temp.length()>=words[j].length())for(int k=0;k<words[j].length();j++){if(temp.charAt(i+k)==words[j].charAt(k))check++;}

You actually increment j instead of k here:

    for(int k=0;k<words[j].length();j++)

And because j corresponds to words.length (which is 3) when you type the third character it tries to reference words[3] which does not exist. I would suggest changing it to:

    for(int k=0;k<words[j].length();k++)

Hope that helps.

Edit:

Now I am thinking that instead of saying:

    if(temp.charAt(i+k)==words[j].charAt(k))check++;

You might want to say:

    if(temp.charAt(i)==words[j].charAt(k))check++;

Upvotes: 2

karllindmark
karllindmark

Reputation: 6071

You're actually incrementing j in the third loop as well, instead of k:

if(temp.length()>=words[j].length())for(int k=0;k<words[j].length();j++)

A spontaneous guess would be that you should change it to the following, to avoid exceeding the max length:

if(temp.length()>=words[j].length())for(int k=0;k<words[j].length();k++)

Upvotes: 5

Related Questions