Java 具有字符串长度的For循环无效

Java 具有字符串长度的For循环无效,java,string-length,Java,String Length,我的程序在执行时出错 错误是: --------------------Configuration: <Default>-------------------- Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.lang.String.charAt(String.java:687) at codeinst

我的程序在执行时出错

错误是:

 --------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
    at java.lang.String.charAt(String.java:687)
    at codeinstaneous.main(codeinstaneous.java:19)
进程已完成。 为什么会发生这种情况?这是我的代码:

import javax.swing.*;

public class codeinstaneous {
    public static void main (String[] args) {
        String theCode;
        theCode = JOptionPane.showInputDialog("Enter the code to decoded it :");
        char theBit;
        char theSource='a';
        int i=0;
        for(i=0;i <= theCode.length() ;i++){
            theBit = theCode.charAt(i);
            if(theBit=='0')
                i++;
        if(theBit=='0')
            theSource='1';
        }    
        JOptionPane.showMessageDialog(null,theSource);
    }    
}
这一行:

for(i=0; i <= theCode.length();i++){
改变

for(i=0; i <= theCode.length(); i++)


编译器肯定没有给出那个错误。而且,因为字符串是基于零的,所以您的条件应该是ifor(i=0; i <= theCode.length(); i++)
for(i=0; i < theCode.length(); i++)