Variable scope basically:
Here the scope of the two dt variables is inside the brackets.
So, generally, you cannot code a conditional variable declaration and
then use that variable outside the scope of the condition....
Code:
if(selectTable==1){
DistanceTable dt = DistanceTable.getDefault(); // <-- "exists" only here
}else if(selectTable==2){
DistanceTable dt = DistanceTable.userTable("test.txt"); // <-- "exists" only here
}else{
System.out.println("Invalid input. Program terminated.");
}
Try this
Code:
DistanceTable dt=null;
if(selectTable==1){
dt = DistanceTable.getDefault();
}else if(selectTable==2){
dt = DistanceTable.userTable("test.txt");
}else{
System.out.println("Invalid input. Program terminated.");
}
And, just to be style-picky, the if statement should have been coded as a
switch statement with two cases and a default.