【问题标题】:Java - ternary Operator not working. Compiler stating it's not a statement [duplicate]Java - 三元运算符不起作用。编译器声明它不是一个声明[重复]
【发布时间】:2018-07-09 07:55:10
【问题描述】:
(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;} 
        }
   }
}

【问题讨论】:

  • 你能显示更多的代码,以及确切的错误信息吗?
  • 编译器准确地告诉你出了什么问题:条件 ?: 表达式不是语句,因此不能用作语句。现在你为什么认为这会“在效率方面更好”?
  • (admin_accounts.add(username)) 是否返回 void
  • 里面有void方法吗?
  • (顺便说一句,我还强烈建议您开始遵循 Java 命名约定。)

标签: java ternary-operator


【解决方案1】:

三元运算符表达式不是语句:您不能单独使用它们来替代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);
}

;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2019-10-30
    • 2013-05-12
    相关资源
    最近更新 更多