【发布时间】: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