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;
}
}
}
}