Hello,
I am getting out of bound error whenever i run the program and trying to add a Node (this would be in my switch). but the error that i am getting is just above my switch statement. Can anyone help me to figure out what it is that i am doing wrong?

import java.util.*;
public class BSTTest {

 
    public static void main(String[] args)
    {
        // Creating Scanner object
        Scanner in = new Scanner(System.in);
        // Local variables
        boolean done  = false;
        char choice;
        String s;
        

        // Constructor call
        BinarySearchTree tree =  new BinarySearchTree();
        tree.DummyNode(29);     // calling on the Dummy Node


        while (!done)   // While statemnt that allows user to keep making selections until requested otherwise
        {
       System.out.println(" Please make a selection:");    // Requesting user to make selection
       s= in.nextLine();        // Reading input from the user as a String
       choice = s.charAt(0);    // Converting String to a Char

       // Swicth statement with selction alternatives
       switch(choice)
        {
            case 'I': 
            case 'i':

                int num;
                System.out.println("Please enter the value of the Node you want to add:");   // Reading user input
                num = in.nextInt();
                tree.insertRoot(num);  // This method is not working well. Only allows to input number once or twice and then something wrong happens
               // tree.search(29);
               // tree.print();
                break;
            case 'D':
            case 'd':
                tree.delete(1);
                System.out.println("test for D");
                break;
            case 'P':
            case 'p':
                tree.print();
                break;
            case 'Q':
            case 'q':
                done = true;
                System.out.println("test for Q");
                System.out.println("Thank you program has exited");
                break;
            default:
               System.out.println("You haven't made right selection. Please re-enter");
               break;
        }
    }
  }
}

Recommended Answers

All 5 Replies

Maybe s contains a zero-length String (""), so there is no char at (0)

Yes but wouldn't grisha have realized that? The only way to get a 0 length String there is to just hit enter....

hehe, i did hit enter. I am not sure if it will help you but the problem disappears if i remove reading of the user input in Switch in case 'I' and place it in my method. In fact, this is where I intended to have it in the beginning (i thought this would be cleaner). Well then i had another error popping up which only allowed me to enter up to 2 Nodes max but at least the program worked.

If your insertRoot method isn't working, and you want help with it, you'll have to post all the relevant code

Well its working now after I moved the prompt from the user to input Node values to my main. But anyway, here is the entire program:
// Node

public class Node {
    public int data;
    public Node left;
    public Node right;

    public Node()   // No argumennt constructor initializing the variables
    {
        data = 0;   // Node value
        left = null; // Left Node
        right = null; // Right Node
    }
    public Node(int val)   // One argumennt constructor initializing the variables
    {
     data = val; // Node value
     left = null;  // Right Node
     right = null; // Left Node
    }

// Class BinarySearchTree

public class BinarySearchTree {

    private Node root;

    public BinarySearchTree() {
        root = null;
    }

    public Node DummyNode(int val) // Dummy Node with initial value of any number
    {
        if (root == null) {
            root = new Node(val);
        } else {
            insertRoot(val);
        }

        return root;
    }

    public int insertRoot(int num) // Insertion of a new Node with value
    {


        if (num > root.data) // When new Node is smaller then root
        {
            if (root.right == null) {
                root.right = new Node(num);

            } else {
                root = root.right;
                insertRoot(num);
            }
        }
        if (num < root.data) // When new Node is larger then root
        {
            if (root.left == null) {
                root.left = new Node(num);
            } else {
                root = root.left;
                insertRoot(num);
            }
        }
        return num;
    }

    public void print() // Printing Nodes in-order
    {
        System.out.print("Nodes organized in 'in-order' are: ");
        moveToNext(root);
    }

    public void moveToNext(Node root) // Traversing in-order
    {
        if (root == null) {
            return;
        }

        if (root.left != null) {
            moveToNext(root.left);
        }

        System.out.print(root.data + "    ");
        if (root.right != null) {
            moveToNext(root.right);
        }
    }

    public void delete(int value) // To delete the Node
    {
        Node parent = null;
        //  target value equals to the root value
        if (value == root.data) {
            Node cur = root;
            // Right side of the tree
            cur = cur.right;

            // If there are a children
            while (cur.left != null) {
                parent = cur;
                cur = cur.left;
            }
            // If there is a child on the right
            cur.left = root.left;
            cur.right = root.right;
            parent.left = null;
            root = cur;
        }
    }
}

// Main

import java.util.*;
public class BSTTest {

 
    public static void main(String[] args)
    {
        // Creating Scanner object
        Scanner in = new Scanner(System.in);
        // Local variables
        boolean done  = false;
        char choice;
        String s;
        

        // Constructor call
        BinarySearchTree tree =  new BinarySearchTree();
        tree.DummyNode(29);     // calling on the Dummy Node


        while (!done)   // While statemnt that allows user to keep making selections until requested otherwise
        {
       System.out.println(" Please make a selection:");    // Requesting user to make selection
       s = in.nextLine();        // Reading input from the user as a String
       choice = s.charAt(0);    // Converting String to a Char
     
       // Swicth statement with selction alternatives
       switch(choice)
        {
            case 'I': 
            case 'i':

                int num;
                System.out.println("Please enter the value of the Node you want to add:");   // Reading user input
                num = in.nextInt();
                tree.insertRoot(num);  // This method is not working well. Only allows to input number once or twice and then something wrong happens
               // tree.search(29);
               
                break;
            case 'D':
            case 'd':
                tree.delete(1);
                System.out.println("test for D");
                break;
            case 'P':
            case 'p':
                tree.print();
                break;
            case 'Q':
            case 'q':
                done = true;
                System.out.println("test for Q");
                System.out.println("Thank you program has exited");
                break;
            default:
               System.out.println("You haven't made right selection. Please re-enter");
               break;
        }
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.