2016-03-23 27 views
1

Okay, ich habe mir einige Stack-Overflow-Fragen und etliche Blogs angeschaut und kann immer noch keine Antwort darauf finden. Nein, es sieht nicht so aus, als ob es geschweifte Klammern, zusätzliche Strichpunkte oder andere Tippfehler gibt, die mir bekannt sind. Warum zeigt das 'sonst' einen Fehler? HierJava: Syntaxfehler auf Token "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 
    } 
} 

ist der Fehler in der Konsole:

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(); 

Antwort

0

die Anzahl der if und else-Anweisung ist nicht gleich. Sie haben 4 wenn Blöcke, aber 5 andere blockieren.

+0

Einer dieser else Blöcke dauert eine Weile. Ich habe den Eindruck, dass es in Ordnung ist, diese Art von Syntax zu verwenden. Liege ich falsch? – Tyrelius

+0

@TyB Nein, 'else' geht nur nach' if'. – EJP

+0

nein .. du kannst sonst nicht mit – stinepike

0

else kann nicht nach while verwendet werden. Es gibt nichts in diesem else Block, also sollte es nur gelöscht werden.

Es gibt eine Menge redundanten Code hier. Meine Rewrite:

// 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 
    } 
} 
+0

Dieser Else-Block sollte Code zum Löschen des Knotens haben, da kein Elternteil mit einer passenden ID gefunden werden konnte. Es ist immer noch in Arbeit. Der Code ist sehr unordentlich, und ich musste Kommentare hinterlassen, um den Überblick darüber zu behalten, was ich damit mache. – Tyrelius

+0

Ich mag die Art und Weise, wie Sie das If innerhalb der If und das Ternär entfernt. Ich denke, ich werde das in diesen Code eingliedern. – Tyrelius

+0

@TyB Sie können diesen Fall mit 'if (! ReorgTopicsIter.hasNext()) {...} else behandeln, während ...' vor der 'while' Schleife. – EJP

0

fünf else mit vier if, nicht überein. Mindestens if sollte mehr als else.

Verwandte Themen