Java - 三元运算符无法正常工作。编译器声明它不是声明

时间:2018-01-30 12:16:57

标签: java ternary-operator

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

说明它是not a statement的信息;这可以用if语句完成,但如果使用三元运算符完成效率会更好;

如果有人能告诉我这个陈述有什么问题。我真的很感激。

功能代码:https://hastebin.com/heriqoripi.vbs

有关该功能的信息  o - 一个类有一个充满字符串的数组,使用for-each循环切割信息以创建帐户(我还没有进行加密,因为我还不知道如何进行散列加密。)

private void update_accounts() throws IOException{
    String[] contents_Of_File = fileHandling.retrieve_contents();
    String username =  "";
    String password = "";
    String unprocessed_information = "";
    int char_count = 0;
    account_information = new HashMap<>();

    if(contents_Of_File == null){
        System.out.println("The contents of the file is empty" );}
    else{
        for(String S : contents_Of_File){
            if(S != null){
                for(char c : S.toCharArray()){
                    char_count++;
                    if(c == delimiter || c == fileHandling.getDelimiter()){
                        if(unprocessed_information.contains("username:")){username = unprocessed_information.replace("username:", "");}
                        if(unprocessed_information.contains("password:")){password = unprocessed_information.replace("password:","");}
                        unprocessed_information="";}
                    else if(char_count == S.length()){
                        unprocessed_information += c;
                        if(unprocessed_information.contains("type:")){
                            ( unprocessed_information.replace("type:","").equals("teacher") ) ? (admin_accounts.add(username)) : (null);
                        }
                        unprocessed_information="";
                        }
                    else{
                        unprocessed_information += c;}
                    }

                if(! account_information.containsKey(username)){
                    System.out.println("o SYSTEM - username:" + username + ", password:" + password + " - have been inputted into the databse.");}
                account_information.put(username, password);
                unprocessed_information = "";
                char_count = 0;}
            else{break;} 
        }
   }
}

1 个答案:

答案 0 :(得分:0)

三元运算符表达式不是语句:您不能单独使用它们来替换if

你应该写

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

为:

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

或(更好,因为第一个if但第二个if没有任何内容):

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

或(恕我直言,甚至更好,因为方式更清晰):

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

- )