gpt4 book ai didi

java - ArrayIndexOutOfBounds,无法定位其原点

转载 作者:行者123 更新时间:2023-11-30 08:56:26 25 4
gpt4 key购买 nike

我写了一个 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();
}
}

最佳答案

您的 while 条件顺序错误,首先检查 i 是否在范围内,然后检查空格。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com