samedi 27 février 2021

Java "Syntax error on token "else", delete this token" error on if else-if else-if clause?

I made a program to order 3 names in lexicographic order. I'm getting the syntax error with the two else if clauses. I can't figure out how I'm supposed to do it without the else-if statements. What is wrong here?

Scanner splitter = new Scanner(names);
    String name1 = splitter.next(); 
    String name2 = splitter.next(); 
    String name3 = splitter.next();



    if (name1.compareToIgnoreCase(name2) < 0 && name1.compareToIgnoreCase(name3) < 0); { 
        namesOrdered = namesOrdered + name1 + " "; 
        if (name2.compareToIgnoreCase(name3) < 0) { 
            namesOrdered = namesOrdered + name2 + " "; 
            namesOrdered = namesOrdered + name3 + " "; 
        }
        else { 
            namesOrdered = namesOrdered + name3 + " "; 
            namesOrdered = namesOrdered + name2 + " ";
        }
        
    }  
    else if (name2.compareToIgnoreCase(name1) < 0 && name2.compareToIgnoreCase(name3) < 0); { 
        namesOrdered = namesOrdered + name2 + " "; 
        if (name1.compareToIgnoreCase(name3) < 0) { 
            namesOrdered = namesOrdered + name1 + " "; 
            namesOrdered = namesOrdered + name3 + " ";
        }
        else {
            namesOrdered = namesOrdered + name3 + " "; 
            namesOrdered = namesOrdered + name1 + " "; 
        }
    }
    else if (name3.compareToIgnoreCase(name2) < 0 && name3.compareToIgnoreCase(name1) < 0); { 
        namesOrdered = namesOrdered + name3 + " "; 
        if (name2.compareToIgnoreCase(name1) < 0) { 
            namesOrdered = namesOrdered + name2 + " "; 
            namesOrdered = namesOrdered + name1 + " "; 
        }
        else { 
            namesOrdered = namesOrdered + name1 + " "; 
            namesOrdered = namesOrdered + name2 + " "; 
        }
    }
    System.out.println(namesOrdered);

Aucun commentaire:

Enregistrer un commentaire