"Syntax error on token" else ", delete this token"

1

Hi, I'm new to java and I've made this code, but in the token else I get the error "Syntax error on token" else ", delete this token", I do not know if someone can help me

Code:

@EventHandler(priority = EventPriority.HIGH)
public boolean onPlayerCommand(PlayerCommandPreprocessEvent event) {
    Player sender = event.getPlayer();
    String mensaje = event.getMessage();
    String ip = event.getPlayer().getAddress().getHostName();
    if (event.getMessage().contentEquals("test"))
    if (event.getMessage().equals("/gamemode 1"))
    if (event.getMessage().equals("/gamemode 2"))
    if (event.getMessage().equals("/gamemode 3"))
    event.getPlayer().getAddress().getHostString();
    String ip1 = event.getPlayer().getAddress().getHostName();
    InetSocketAddress ipposta = event.getPlayer().getAddress();
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "ban " + event.getPlayer().getName());
    Bukkit.getConsoleSender().sendMessage("paper security executed");
    Bukkit.getConsoleSender().sendMessage("beta"); {
    Bukkit.getConsoleSender().sendMessage(ip1 + "la ip del jugador es la mencionada, #papersecurity");
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "bc " + event.getPlayer().getAddress());
    return false;

    }else{
    event.setCancelled(true);
    return true; }
}

}
    
asked by ImSanti_ 15.07.2018 в 02:06
source

1 answer

2

Your problem is simple, you have two errors but when solving the basic you solve the derivative :

1-The first is that as a good programming practice it is recommended that the "if (...)" always carry keys and one of the reasons is to avoid your error.

If(...){
    Una única línea de código o múltiples; ....
}else{
    Una única línea de código o múltiples; ....
}

But Java also allows you to make keyless statements of the type:

If(...) 
    Una única línea de codigo;
else 
    Una única línea de código;

Now is when the problem comes. If you encounter the following situation as is your case:

If(...) 
    If(...)
        Una única línea de código;

The above is still perfectly valid, but the problem comes when you add an else:

If(...) 
    If(...)
        Una única línea de código;
    else {
      Una única línea de código o múltiples; ....
     }

ERROR: The else at compile time does not know what if it has to be associated, does it go to the first ?, does it go to the second? How does the compiler identify it? He can not.

CORRECT CODE:

If(...){ 
    If(...){
        Una única línea de código o múltiples; ....
    } else {
      Una única línea de código o múltiples; ....
    }
}

Ó

If(...){ 
    If(...){
        Una única línea de código o múltiples; ....
    }
} else {
      Una única línea de código o múltiples; ....
}

That is why in these situations you have to put keys always to avoid the problem.

2-The second error is the closing of keys, the only key that is open is that of Bukkit.getConsoleSender().sendMessage("beta"); { so when closing it the Java compiler detects that it is not a conditional statement and does not support the else when closing the key.

Bukkit.getConsoleSender().sendMessage("beta"); { 
   Código....; 
} else { 
    Código....; 
}

IT IS NOT VALID.

Fix error 1 and you will not have to worry about this one.

I hope I have helped you.

    
answered by 15.07.2018 в 11:10