0

I'm trying to make a code that is passing a string into AVLTree.java but I'm getting the Index 1 out of bounds for length 0 at Driver.main(Driver.java:17) error. Driver main:

import java.io.*;
import java.util.Scanner;

public class Driver {

public static void main(String[] args) throws IOException  {
    
    
    BufferedReader in = new BufferedReader(new FileReader("Booklist.txt"));
    String line;
        AVLTree bst = new AVLTree();
        while((line = in.readLine()) != null) {
             String values[] = line.split(" ");

              Book book = new Book(values[1],values[2]);
        
            bst.insert(values[0]);
            
        }
        
        
        in.close();
    /*  
        bst.insert(1);
        bst.insert(2);
        bst.insert(3);
        bst.insert(4);
        bst.insert(5);
        bst.insert(6);
        bst.insert(7);
        bst.insert(8);
        bst.prettyPrint();
        */
    }
}

.....

AVLTree insert function.

public void insert(String isbn) {
    AVLNode node = new AVLNode(isbn,  null);
    AVLNode y = null;
    AVLNode x = this.root;

    while (x != null) {
        y = x;
        if (node.ISBN.compareTo(x.ISBN)<0) {
            x = x.left;
        } else {
            x = x.right;
        }
    }
    node.parent = y;
    if (y == null) {
        root = node;
    } else if (node.ISBN.compareTo(y.ISBN)<0 ) {
        y.left = node;
    } else {
        y.right = node;
    }
    updateBalance(node);
}

The txt I'm using looks like this

enter image description here

cornel
  • 1
  • 1

1 Answers1

0

The problem is clearly related to the split(" ") statement in some case when a line cannot be splitted because it has no whitespace character.

String values[] = line.split(" ");
Book book = new Book(values[1],values[2]); <-- Here you access at position 1

Looking at the text input example you shown, it seems that it has an empty line. It might be the 1st or the last one.

Leo Tapia
  • 131
  • 6