Java 在if语句中创建对象并稍后使用

Java 在if语句中创建对象并稍后使用,java,object,if-statement,Java,Object,If Statement,我正在为中缀符号编写解析器。 在if语句中,我声明了变量newchild。否则我希望它抛出一个异常。但是当我超出范围时,编译器就不再知道变量了。 我不能在if语句之前声明它,因为根据我们所处的情况,变量被分配了不同的数据类型 我能做些什么来解决这个问题 public class ParserForInfixNotation { public Node parse(List<String> tokenList) { Stack<String> myStack =

我正在为中缀符号编写解析器。 在if语句中,我声明了变量newchild。否则我希望它抛出一个异常。但是当我超出范围时,编译器就不再知道变量了。 我不能在if语句之前声明它,因为根据我们所处的情况,变量被分配了不同的数据类型

我能做些什么来解决这个问题

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; 
}
你可以做:

int value = = -1;
Leaf res = null;
if(Character.isDigit(tokenList.get(i).charAt(1))){
      value =Integer.parseInt(tokenList.get(i));                
      res = new Leaf(value);
 }
并检查变量value和res中存储的值是否与范围有关。变量的作用域是在其中声明它的块,以及该块中的块。无论该块是if语句的块、其他语句的块还是为了定义作用域而放在那里的块

您正在经历以下情况:

{
    Leaf leaf = new Leaf();
}

doSomethingWith(leaf); // compiler error - there is no `leaf` in this scope.
您可以通过以下方式进行修复:

 Leaf leaf;
 {
      leaf = new Leaf();
 }
 doSomethingWith(leaf);
如果对leaf的赋值可能不会发生——例如,如果它在If块中,那么您将得到一个编译器错误,说明变量leaf可能尚未初始化。您可以通过先初始化为某个回退值来修复此问题。它通常为空:

 Leaf leaf = null;
 if(...) {
      leaf = new Leaf();
 }
 doSomethingWith(leaf);

但是现在您的代码必须处理leaf==null的可能性,这会导致代码过于复杂或脆弱。找到避免赋值null的方法,或者如果不能,将易受攻击的代码块隔离,这样在该范围之外就不需要处理null变量的可能性。

一种方法是移动/复制此行:

UnaryOpNode res = new UnaryOpNode('-',newchild);
因此它包含在If和else If中,如下所示:

if((Character.isDigit(tokenList.get(j).charAt(1))) || (tokenList.get(j+1) == ")"))
{
    Leaf newchild = new 
    Leaf(Integer.parseInt(tokenList.get(j)));
    UnaryOpNode res = new UnaryOpNode('-',newchild);
}
else if(tokenList.get(j) == "("){
    while(!end){
        if(tokenList.get(j) == ")" && anzahlklammern == 1){
            end = true;
        }
        else if(tokenList.get(j) == ")" && j > i+3){   
            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);
    UnaryOpNode res = new UnaryOpNode('-',newchild);
}
else {
    throw new IllegalArgumentException();
}

在您希望能够在其中使用它的任何范围中声明它。您可以在if语句中为它赋值,也可以不赋值,然后在使用它时对照空值进行检查。为什么不在if块中移动该值?叶和节点之间的关系是什么。若存在父子关系,那个么只需在If块外部声明一个父类型的变量,并在块内部初始化它