[Solved] Java – ternary Operator not working. Compiler stating it’s not a statement [duplicate]


Ternary operator expressions are not statements: you can’t use them on their own as a replacement for an if.

You should write

if (unprocessed_information.contains("type:")) {
    ( unprocessed_information.replace("type:","").equals("teacher") ) ? (admin_accounts.add(username)) : (null);
}

as:

if (unprocessed_information.contains("type:")) {
    if (unprocessed_information.replace("type:","").equals("teacher")) admin_accounts.add(username);
}

or (better, since there is nothing in the first if but the second if):

if (    unprocessed_information.contains("type:")
     && unprocessed_information.replace("type:","").equals("teacher") ) {
    admin_accounts.add(username);
}

or (IMHO even better, as it is way clearer):

if (   unprocessed_information.equals("type:teacher")
    || unprocessed_information.equals("ttype:eacher") 
    || unprocessed_information.equals("tetype:acher") 
    || unprocessed_information.equals("teatype:cher") 
    || unprocessed_information.equals("teactype:her") 
    || unprocessed_information.equals("teachtype:er") 
    || unprocessed_information.equals("teachetype:r") 
    || unprocessed_information.equals("teachertype:") ) {
    admin_accounts.add(username);
}

😉

solved Java – ternary Operator not working. Compiler stating it’s not a statement [duplicate]