Cree un objeto en la declaración if y úselo más tarde

Estoy escribiendo un analizador para notación de infijo. En la declaración if declaro la variable newchild. De lo contrario, quiero que arroje una excepción. Pero cuando estoy fuera del alcance, el compilador ya no conoce la variable. No puedo declararlo antes de la declaración if, porque a la variable se le asigna un tipo de datos diferente según el caso en el que nos encontremos.

¿Qué puedo hacer para arreglar esto?

public class ParserForInfixNotation {

public Node parse(List<String> tokenList) {

    Stack<String> myStack = new Stack<String>();
    int i =1;
    while(i <= tokenList.size()){                           //wir gehen alle Eintraege in der Liste durch
        if(Character.isDigit(tokenList.get(i).charAt(1))){
            int value = Integer.parseInt(tokenList.get(i));     //falls der Eintrag eine Zahl ist, wird ein neuer Leaf erstellt
            Leaf res = new Leaf(value);
        }
        else if(tokenList.get(i) == "("){                       // falls der Eintrag eine Klammer ist, wird geschaut, ob in der Klammer ein Unary oder Binary OpNode definiert ist
            if (tokenList.get(i+1) == "-") {
                                                                // Fall Unary
                int j = i+1;
                                                                //am Liebsten ein rekursiver Aufruf auf parser mit nem TeilString des Oberen Strings, der genau den naechsten Node beschreibt
                int anzahlklammern = 0;
                boolean end = false;
                if((Character.isDigit(tokenList.get(j).charAt(1))) || (tokenList.get(j+1) == ")")){
                    Leaf newchild = new Leaf(Integer.parseInt(tokenList.get(j)));
                }
                else if(tokenList.get(j) == "("){
                    while(!end){
                        if(tokenList.get(j) == ")" && anzahlklammern == 1){
                            end = true;
                        }
                        else if(tokenList.get(j) == ")" && j > i+3){                    //die Klammer muss mindestens 2 Stellen enthalten
                            anzahlklammern--;
                            j++;
                        }
                        else if(tokenList.get(j) == "("){
                            anzahlklammern++;
                            j++;
                        }
                        else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
                            j++;
                        }
                        else{
                            throw new IllegalArgumentException();
                        }
                    }
                    List<String> neu = new ArrayList<>();

                    for (int l = i+2; l<j;l++){
                        neu.add(tokenList.get(l));
                    }
                    Node newchild = parse(neu);
                }
                else {
                    throw new IllegalArgumentException();
                }

                UnaryOpNode res = new UnaryOpNode('-',newchild);
            }
            else if((tokenList.get(i+1) == "(") || (Character.isDigit(tokenList.get(i+1).charAt(1)))){ //Fall Binary
                if (Character.isDigit(tokenList.get(i+1).charAt(1)) && (tokenList.get(i+2) == "+" || tokenList.get(i+2) == "*")){
                    Leaf newchildleft = new Leaf(Integer.parseInt(tokenList.get(i+1)));
                    if(tokenList.get(i+2) == "+"){
                        Character operator = '+';
                    }
                    else if(tokenList.get(i+2) == "*"){
                        Character operator = '*';
                    }
                    int j = i+3;
                    if(Character.isDigit(tokenList.get(j).charAt(1))){
                        Leaf newchildright = new Leaf(Integer.parseInt(tokenList.get(j)));
                    }
                    else if(tokenList.get(j) == "("){
                        boolean end = false;
                        int anzahlklammern =0 ;
                        while(!end){
                            if(tokenList.get(j) == ")" && anzahlklammern == 1){
                                end = true;
                            }
                            else if(tokenList.get(j) == ")" && j > i+5){                    //die Klammer muss mindestens 2 Stellen enthalten
                                anzahlklammern--;
                                j++;
                            }
                            else if(tokenList.get(j) == "("){
                                anzahlklammern++;
                                j++;
                            }
                            else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
                                j++;
                            }
                            else{
                                throw new IllegalArgumentException();
                            }
                        }
                        List<String> neu = new ArrayList<>();

                        for (int l = i+4; l<j;l++){
                            neu.add(tokenList.get(l));
                        }
                        Node newrightchild = parse(neu);
                    }
                    else{
                        throw new IllegalArgumentException();
                    }
                }
                else if(tokenList.get(i+1) == "("){
                    int j= i+1;
                    boolean end = false;
                    int anzahlklammern =0 ;
                    while(!end){
                        if(tokenList.get(j) == ")" && anzahlklammern == 1){
                            end = true;
                        }
                        else if(tokenList.get(j) == ")" && j > i+3){                    //die Klammer muss mindestens 2 Stellen enthalten
                            anzahlklammern--;
                            j++;
                        }
                        else if(tokenList.get(j) == "("){
                            anzahlklammern++;
                            j++;
                        }
                        else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
                            j++;
                        }
                        else{
                            throw new IllegalArgumentException();
                        }
                    }
                    List<String> neu = new ArrayList<>();

                    for (int l = i+2; l<j;l++){
                        neu.add(tokenList.get(l));
                    }
                    Node newleftchild = parse(neu);
                    if(tokenList.get(j+1) == "+"){
                        Character operator = '+';
                    }
                    else if(tokenList.get(j+1) == "*"){
                        Character operator = '*';
                    }
                    else{
                        throw new IllegalArgumentException();
                    }
                    if(tokenList.get(j+2)== "("){
                        int k= j+3;
                        end = false;
                        anzahlklammern =0 ;
                        while(!end){
                            if(tokenList.get(k) == ")" && anzahlklammern == 1){
                                end = true;
                            }
                            else if(tokenList.get(k) == ")" && k > j+5){                    //die Klammer muss mindestens 2 Stellen enthalten
                                anzahlklammern--;
                                k++;
                            }
                            else if(tokenList.get(k) == "("){
                                anzahlklammern++;
                                k++;
                            }
                            else if ((Character.isDigit(tokenList.get(k).charAt(1))) || tokenList.get(k) == "+" || tokenList.get(k) == "*" || tokenList.get(k) == "-"){
                                k++;
                            }
                            else{
                                throw new IllegalArgumentException();
                            }
                        }
                        List<String> neu2 = new ArrayList<>();

                        for (int l = j+4; l<k;l++){
                            neu.add(tokenList.get(l));
                        }
                        Node newrightchild = parse(neu2);
                    }
                    else if(Character.isDigit(tokenList.get(j+2).charAt(1))){
                        Leaf newrightchild = new Leaf(Integer.parseInt(tokenList.get(j+2)));
                    }
                    else{
                        throw new IllegalArgumentException();
                    }
                }
                BinaryOpNode res = new BinaryOpNode(operator, newleftchild, newrightchild);
            }
            else{
                throw new IllegalArgumentException();
            }
        }
        else{
            throw new IllegalArgumentException();
        }
    }



    return res; 
}

Respuestas a la pregunta(3)

Su respuesta a la pregunta