【问题标题】:Java: Syntax error on token "else"Java:标记“else”上的语法错误
【发布时间】:2016-03-23 03:22:46
【问题描述】:

好的,我查看了很多堆栈溢出问题和很多博客,但仍然找不到答案。不,它看起来不像缺少大括号,没有多余的分号,或者我知道的任何其他错字。为什么“else”显示错误?

// check if myid property is set and create it if not
String subtopicMyId = subtopicNode.hasProperty("myid") ? subtopicNode.getProperty("myid").getString() : "";
if (subtopicMyId.equals("")) {
    // generate new myid and check it against the list of existing IDs until a unique one is generated
    do {
        // generate new myid
        Object topicmyidobj = new Object();
        subtopicMyId = topicmyidobj.toString().split("@")[1].toUpperCase();
    } while ( subtopicMyId.equals("") || existingMyIds.contains(subtopicMyId) );
    // set myid on this node
    subtopicNode.setProperty("myid", subtopicMyId);
    subtopicNode.setProperty("parentid", topicMyId);
    subtopicNode.save();
    // add new myid to list of existsing IDs so that it doesn't get reused
    existingMyIds.add(subtopicMyId);
} else {
    // if subtopic has myid already
    // compare the parentid to the parent myid
    String subtopicParentId = subtopicNode.getProperty("parentid").getString();
    if (!subtopicParentId.equals(topicMyId)) {
        // they don't match
        String subtopicNodePath = subtopicNode.getPath();
        String topicNodePath = topicNode.getPath();
        // find path to topic node that has matching myid to this subtopic's parentid
        // loop through parent nodes
        NodeIterator reorgTopicsIter = compNode.getNodes();
        while (reorgTopicsIter.hasNext()) {
            // loop through parent objects to find a matching myid for parentid
            Node reorgTopicNode = (Node)reorgTopicsIter.next();
            // get the myid property from this node, if it exists, and compare the parentid to it
            String reorgTopicMyId = reorgTopicNode.hasProperty("myid") ? reorgTopicNode.getProperty("myid").getString() : "";
            if (!reorgTopicMyId.equals("")) {
                // parent myid exists and is not blank
                if (reorgTopicMyId.equals(subtopicParentId)) {
                    // parentid does match parent myid
                    String reorgTopicNodePath = reorgTopicNode.getPath();
                    // determine how many parent objects there are
                    int reorgTopicSubtopics = 0;
                    NodeIterator reorgSubtopicsIter = reorgTopicNode.getNodes();
                    while (reorgSubtopicsIter.hasNext()) {
                        Node reorgSubtopicNode = (Node)reorgSubtopicsIter.next();
                        reorgTopicSubtopics++;
                    }
                    // set source to this child object
                    String source = subtopicNode.getPath();
                    // set destination to matching parent object with new child object appended
                    String destination = reorgTopicNodePath + "/subtopic-" + (reorgTopicSubtopics + 1);
                    // create session for move and perform move
                    Session session = resourceResolver.adaptTo(Session.class);
                    session.move(source, destination);
                    session.save();
                } else {
                    // parentid does not match parent myid.
                    // nothing we need to do here;
                    // it just moves on to check next parent myid.
                }
            } else {
                // parent myid does not exist or is blank
            }
        } else {
            // no more parent objects to loop through, so we need to check if a match was found
            // if no match was found, then parent was deleted or no longer exists, so we need to remove this child
        }
    } else {
        // parentid does match parent myid
    }
}

这是控制台中的错误:

An error occurred at line: 145 in the jsp file: /apps/covidien/components/content/utilities/faq-node-process/faq-node-process.jsp
Syntax error on token "else", delete this token
142:                                 subtopicNode.save();
143:                                 // add new myid to list of existsing IDs so that it doesn't get reused
144:                                 existingMyIds.add(subtopicMyId);
145:                             } else {
146:                                 // if subtopic has myid already
147:                                 // compare the parentid to the parent myid
148:                                 String subtopicParentId = subtopicNode.getProperty("parentid").getString();

【问题讨论】:

    标签: java if-statement syntax


    【解决方案1】:

    if 和 else 语句的数量不相同。你有 4 个 if 块,但 5 个 else 块。

    【讨论】:

    • 其中一个块会持续一段时间。我一直认为使用这种语法是可以的。我错了吗?
    • @TyB 不,else 只在 if 之后。
    • 嗯......我认为这是一种糟糕的形式,但无论如何都同意了建议。我将不得不改变我处理该循环的方式。我一直在考虑设置一个布尔标志,如果找到匹配项则标记为真,然后在循环之后,读回该标志,如果它仍然为假,我知道节点不再有父节点并且可以删除.
    【解决方案2】:

    五个else 与四个if,不匹配。 至少if 的数量应多于else。

    【讨论】:

      【解决方案3】:

      else 不能在while 之后使用。无论如何,else 块中没有任何内容,因此应该将其删除。

      这里有很多冗余代码。我的重写:

      // check if myid property is set and create it if not
      String subtopicMyId = subtopicNode.getProperty("myid").getString();
      if (subtopicMyId == null) {
          // generate new myid and check it against the list of existing IDs until a unique one is generated
          do {
              // generate new myid
              Object topicmyidobj = new Object();
              subtopicMyId = topicmyidobj.toString().split("@")[1].toUpperCase();
          } while ( subtopicMyId.equals("") || existingMyIds.contains(subtopicMyId) );
          // set myid on this node
          subtopicNode.setProperty("myid", subtopicMyId);
          subtopicNode.setProperty("parentid", topicMyId);
          subtopicNode.save();
          // add new myid to list of existsing IDs so that it doesn't get reused
          existingMyIds.add(subtopicMyId);
      } else {
          // if subtopic has myid already
          // compare the parentid to the parent myid
          String subtopicParentId = subtopicNode.getProperty("parentid").getString();
          if (!subtopicParentId.equals(topicMyId)) {
              // they don't match
              String subtopicNodePath = subtopicNode.getPath();
              String topicNodePath = topicNode.getPath();
              // find path to topic node that has matching myid to this subtopic's parentid
              // loop through parent nodes
              NodeIterator reorgTopicsIter = compNode.getNodes();
              while (reorgTopicsIter.hasNext()) {
                  // loop through parent objects to find a matching myid for parentid
                  Node reorgTopicNode = (Node)reorgTopicsIter.next();
                  // get the myid property from this node, if it exists, and compare the parentid to it
                  String reorgTopicMyId = reorgTopicNode.getProperty("myid").getString();
                  if (reorgTopicMyId != null && reorgTopicMyId.equals(subtopicParentId)) {
                      // parentid does match parent myid
                      String reorgTopicNodePath = reorgTopicNode.getPath();
                      // determine how many parent objects there are
                      int reorgTopicSubtopics = 0;
                      NodeIterator reorgSubtopicsIter = reorgTopicNode.getNodes();
                      while (reorgSubtopicsIter.hasNext()) {
                          Node reorgSubtopicNode = (Node)reorgSubtopicsIter.next();
                          reorgTopicSubtopics++;
                      }
                      // set source to this child object
                      String source = subtopicNode.getPath();
                      // set destination to matching parent object with new child object appended
                      String destination = reorgTopicNodePath + "/subtopic-" + (reorgTopicSubtopics + 1);
                      // create session for move and perform move
                      Session session = resourceResolver.adaptTo(Session.class);
                      session.move(source, destination);
                      session.save();
                  } else {
                          // parentid does not match parent myid.
                          // nothing we need to do here;
                          // it just moves on to check next parent myid.
                  }
              }
          } else {
              // parentid does match parent myid
          }
      }
      

      【讨论】:

      • 那个 else 块将有一些用于删除节点的代码,因为找不到具有匹配 id 的父节点。它仍在进行中。代码非常混乱,我不得不在整个过程中留下 cmets 来跟踪我正在用它做什么。
      • 我喜欢你在 if 中重做 if 并删除三元组的方式。我想我要把它重新合并到这段代码中。
      • @TyB 您可以在相关的while 循环之前使用if (!reorgTopicsIter.hasNext()) { ... } else while ... 处理这种情况。
      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 2014-08-30
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多