(Using Java) I am implementing a generic class which is a B-Tree. When the user runs the program they can supply some arguments which will determine the type of the tree (Integer, Character, Double or String).
In my main method I have this code:
// Get user input and split it into tokens
// Tokens[1] = the type specified by the user
if( tokens[1].equals("DOUBLE"))
BTree<Double> t = new BTree<Double>();
else if( tokens[1].equals("CHARACTER"))
BTree<Character> t = new BTree<Character>();
else if( tokens[1].equals("INTEGER"))
BTree<Integer> t = new BTree<Integer>();
else if( tokens[1].equals("STRING"))
BTree<String> t = new BTree<String>();
But the compiler complains if I have the if statements. If I remove them then it compiles and runs fine :/ how can I fix this? So that the user can supply the type and the tree will be created depending on the type supplied? Thanks.
Here is some of the compiler output:
C:\Users\User\Desktop>javac *.java
Main.java:42: error: cannot find symbol
BTree<Double> t = new BTree<Double>();
symbol: variable BTree
location: class Main
Main.java:42: error: cannot find symbol
BTree<Double> t = new BTree<Double>();
symbol: variable Double
location: class Main
Main.java:42: error: cannot find symbol
BTree<Double> t = new BTree<Double>();
symbol: variable t
location: class Main
.. There is more but it is similar and repeated for each of the types
The variable
tin each of theifstatements is only defined in theif‘s context, outside them the compiler won’t recognize them.Define your variable
tbefore theifstatements.