Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
arrayOutOfBounds异常java_Java_Arrays - Fatal编程技术网

arrayOutOfBounds异常java

arrayOutOfBounds异常java,java,arrays,Java,Arrays,我想解析一个文本文件,并根据addToTree方法中指定的规则从中构建一个树。但是,我遇到了以下错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at ie.gmit.TreeTest.addToTree(TreeTest.java:27) at ie.gmit.TreeTest.parse(TreeTest.java:20) at ie.gmit.TreeTest.main(TreeTest.j

我想解析一个文本文件,并根据addToTree方法中指定的规则从中构建一个树。但是,我遇到了以下错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ie.gmit.TreeTest.addToTree(TreeTest.java:27)
at ie.gmit.TreeTest.parse(TreeTest.java:20)
at ie.gmit.TreeTest.main(TreeTest.java:77)
addChar1和addChar2是我通过在parse方法中传入单词创建的节点

代码如下:

public class TreeTest {

public void parse(File f) throws Exception {
    Node root = new  Node('+'); //create a root node
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));

    String line;
    while((line = br.readLine())!=null){
        String[] words = line.toLowerCase().split(" ");

        for(int i = 0; i < words.length; i++){
            addToTree(words[i], root);
        }
    }
}

public void addToTree(String s, Node root){
    char[] characters = s.toCharArray();
    Node addChar1 = new Node(characters[0]);
    Node addChar2 = new Node(characters[1]);
    Node fullWord = new Node(s);

    //get the child nodes of the root
    Node[] rootChildren = root.children();
    //get the child nodes of the first node (addChar1)
    Node[] addChar1Children = addChar1.children();

    //get each child of the root
    for(int i=0; i<rootChildren.length; i++){
        Node rootChild = rootChildren[i];
        //see if the addChar1 already exists in the tree
        //if it doesn't
        if(!rootChild.equals(addChar1)){
            //add the addChar1 as a child of the root
            root.addChild(addChar1);
            //add the addChar2 as a child of the addChar1 also
            addChar1.addChild(addChar2);
            //insert the whole word as the child of the addChar2
            addChar2.addChild(fullWord);
        }
        //if the addChar1 exists in the tree already
        else{
            // get each child of the addChar1
            for(int j=0; j<addChar1Children.length; j++){
                Node addChar1Child = addChar1Children[i];
                //see if the addChar2 already exists in the tree
                //if it doesn't
                if(!addChar1Child.equals(addChar2)){
                    //add the addChar2 as the child if the addChar1
                    addChar1.addChild(addChar2);
                    //add the actual word
                    addChar2.addChild(fullWord);
                }
                //if the addChar2 exists the the tree already
                else{
                    //insert the whole word as the child of the FOUND NODE
                    addChar1Child.addChild(fullWord);
                }
            }//end of second for loop
        }
    }//end of the first for loop

}//end of addToTree

 public static void main(String[] args) throws Exception {
     TreeTest test = new TreeTest();

     File f = new File("textFile.txt");
     test.parse(f);
 }
公共类树测试{
公共void解析(文件f)引发异常{
节点根=新节点(“+”);//创建根节点
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(f));
弦线;
而((line=br.readLine())!=null){
String[]words=line.toLowerCase().split(“”);
for(int i=0;i
}检查这一行(27):


正在传递的
字符串是否包含任何字符?请检查输入文件中是否没有空行。

在访问索引0和1之前,您可能应该检查它们:

if (!s.isEmpty()) {
       char[] characters = s.toCharArray();
       Node addChar1 = new Node(characters[0]);
       Node addChar2 = new Node(characters[1]);
...

显然,您有一些空字符串。

当您使用此代码时

Node addChar1 = new Node(characters[0]);
Node addChar2 = new Node(characters[1]);

在访问索引元素之前,您应该检查字符数组的大小。当字符数组大小小于2时,您将获得ArrayIndexOutOfBoundsException

char[]characters=s.ToCharray();//检查字符数组长度,然后写入字符[1]

您确定您的流程没有从输入文件中提取空字符串吗?明显的罪魁祸首是您假设输入字符串的长度均大于等于2。实际上,文件中的所有单词的长度均大于等于2length@ciastkoo-文件中的所有单词长度可能大于等于2,但您仍然可以从文件中提取空字符串,取决于您的操作方式。您可能需要检查方法中输入字符串的长度,以确保。@Perception您能解释一下我如何提取空字符串吗?@ciastkoo您的输入文件中有空行吗?该文件不包含纵向为3+的单词,您说它不包含纵向为3+的单词长度。空单词或有1或2个字符的单词怎么样?
if (!s.isEmpty()) {
       char[] characters = s.toCharArray();
       Node addChar1 = new Node(characters[0]);
       Node addChar2 = new Node(characters[1]);
...
Node addChar1 = new Node(characters[0]);
Node addChar2 = new Node(characters[1]);