Create Object in if-statement and use it later

前端 未结 3 1804
[愿得一人]
[愿得一人] 2021-01-26 09:41

I am writing a Parser for Infix Notation. In the if-statement I declare the variable newchild. Otherwise I want it to throw an exception. But when I am out of the scope the Comp

相关标签:
3条回答
  • 2021-01-26 09:52

    This is about scope. A variable's scope is the block in which it is declared, and blocks within that. It doesn't matter whether the block is the block of an if statement, some other statement, or just a block put there for the purpose of defining scope.

    You are experiencing this:

    {
        Leaf leaf = new Leaf();
    }
    
    doSomethingWith(leaf); // compiler error - there is no `leaf` in this scope.
    

    You can fix it with:

     Leaf leaf;
     {
          leaf = new Leaf();
     }
     doSomethingWith(leaf);
    

    If there is a possibility that the assignment to leaf won't happen -- for example if it's in an if block, then you'll get a compiler error saying variable leaf may not have been initialized. You can fix this by initialising to some fallback value first. It's often null:

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

    But now your code has to cope with the possibility that leaf == null - and this leads to code that's either over-complex or fragile. Find ways to avoid ever assigning null, or if you can't, keep the vulnerable block of code isolated, so that nothing outside that scope needs to deal with the possibility of a null variable.

    0 讨论(0)
  • 2021-01-26 10:04

    One way would be to move / copy this line :

    UnaryOpNode res = new UnaryOpNode('-',newchild);
    

    So that it is contained within both the If and the else if like this:

    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();
    }
    
    0 讨论(0)
  • 2021-01-26 10:05

    you can do:

    int value = = -1;
    Leaf res = null;
    if(Character.isDigit(tokenList.get(i).charAt(1))){
          value =Integer.parseInt(tokenList.get(i));                
          res = new Leaf(value);
     }
    

    and check out of the if what values are stored in the variables value and res

    0 讨论(0)
提交回复
热议问题