我编写了一个类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'。请注意,单词末尾的空格已删除(我的代码中有一种方法)。

我已经研究了很长时间的代码,但仍然找不到“索引超出范围”错误的出处。

我试过运行调试器,看来对于此特定输入"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();
    }
}

最佳答案

如果条件条件顺序错误,请先检查我是否在范围内,然后检查空间。

关于java - ArrayIndexOutOfBounds,无法找到其原点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28612003/

10-12 00:12
查看更多