ArrayIndexOutOfBounds,无法找到它的原点

时间:2015-02-19 16:46:21

标签: java

我编写了一个类TokenizableString,用于标记作为用户输入的字符串。这是一个如何应用的例子

我输入

"My name is methos"

我应该在控制台中看到以下

'My'
'name'
'is'
'methos'

当我输入以下输入时,有一个问题:"Badr " 我收到以下例外:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at exercicesPOO.TokenizableString.<init>(TokenizableString.java:40)
    at exercicesPOO.TokenizableString.main(TokenizableString.java:127)
Java Result: 1

而期望的输出应为:'Badr'。请注意,单词末尾的空格被删除(我的代码中有一个方法)。

我一直在查看我的代码很长一段时间,我仍然无法找到'index out of bounds'错误的来源。

我已经尝试运行调试器,它似乎永远不会超出对此特定输入的构造函数调用:"Badr "

你也会善待我的编码的可读性/质量。任何建议/评论都非常受欢迎。谢谢。

P_S:我正在使用Netbeans。

以下是代码:

package debugging;


import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;

public class TokenizableString {
        static Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
        private String contenu;
        private int from;
        private int len;       

        //Constructeurs
        public TokenizableString(String contenu)
        {  
                this.contenu = contenu;
                System.out.print(this.contenu);
                System.out.println("<--- Ended here");
                this.removeExtraSpaces();
                System.out.print(this.contenu);               
                System.out.println("<--- Ended here");
                this.from=0;
                this.len = 0;
                char[] contenuChar = this.contenu.toCharArray();
                int i = 0;
                do
                {
                        this.len++;
                        i++;
                }while(i<contenuChar.length && contenuChar[i] != ' ');
                // La condition est qu'il faut calculer la longueur de la première séquence de lettres (du premier mot) en itérant soit jusqu'au prochain ' ' (espace) ou jusqu'à atteindre la fin de la phrase.
                //The condition is that we have to calculate the length of the first sequence of letters (first word) by iterating until the next ' ' (blank space) or until we reach the end of the sentence.
        }

        //Methods

        //removeExtraSpaces() removes the spaces that the user would have entered at the end of the inputed string for example : "Badr              " would become "Badr"
        private void removeExtraSpaces()
        {
                boolean Acc = true;
                do
                {
                        if(this.contenu.charAt(this.contenu.length()-1)==' ')
                        {
                                char[] temp = new char[this.contenu.length()-1];

                                for(int i = 0 ; i < this.contenu.length() -1; i++)
                                {                                        
                                        temp[i] = this.contenu.charAt(i);
                                }
                                String tempStr ="";
                                for(int i = 0; i < temp.length; i++)
                                {
                                        tempStr += temp[i];
                                }
                                this.contenu = tempStr;                    
                                if(this.contenu.charAt(this.contenu.length()-1)==' ')
                                {
                                        Acc= false;
                                }
                                else
                                {
                                        Acc = true;
                                        break;
                                }
                        }                        
                }while(Acc == false);
        }


        //nextToken() places the 'from' in the beggining of a word and calculates the length of that given word via 'len'. If there we reach the end of the sentence this method will return false.
        public boolean nextToken()
        {                
                char[] contenuChar = this.contenu.toCharArray();

                if ( (this.from+this.len+1) < contenuChar.length && ( ( this.from == 0 && contenuChar[this.from+this.len] == ' ' ) || ( this.from !=0 && contenuChar[this.from - 1] == ' ' ) ) )
                {                   

                        this.from += (this.len +1);
                        this.len = 0;                       
                        int i = this.from;
                        while( i < contenuChar.length)
                        {
                                if(contenuChar[i] != ' ')
                                {
                                        this.len++;
                                        i++;    
                                }
                                else
                                {
                                        break;
                                }
                        }                        
                        //Nous avons donné une nouvelle valeures aux variables from et len.
                        //System.out.println("from = "+this.from+" || len= "+this.len);
                        return true;
                }
                return false;
        }

        //putting the given words of a sentence via previous method into a dynamic array.
        public void tokenize()
        {
                ArrayList <String> mots = new ArrayList<>();
                do
                {                       
                        String mot = "";
                        for(int i = from; i < (this.len+this.from); i++)
                        {
                                mot += contenu.charAt(i);
                        }
                        mots.add(mot);
                }while(this.nextToken() == true);

                for(String mot : mots)
                {
                        System.out.println("'"+mot+"'");
                }
        }
        public static void main(String[] args) {
                String phrase;
                System.out.println("Entrez une chaine :");
                phrase = scanner.nextLine();
                TokenizableString toToken = new TokenizableString(phrase);
                toToken.tokenize();
    }
}

1 个答案:

答案 0 :(得分:2)

你的条件有错误的顺序,首先检查我是否在范围内,然后检查空间。