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

Information stating that it's not a statement; this can be done with an if-statement but it would be much better in terms of efficiency if it was done using the ternary operator;

If anyone could inform me what is wrong with the statement. I would really appreciate it.

function code: https://hastebin.com/heriqoripi.vbs

information about the function o - A class has an array full of strings which using a for-each loop cuts the information to create accounts ( I have yet to do the encryption because, I don't know how to do hash encryption yet.)

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;} 
        }
   }
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • can you show a bit more code, and the exact error message? – Stultuske Jan 30 '18 at 12:17
  • 7
    The compiler is telling you exactly what's wrong: a conditional ?: expression isn't a statement, so can't be used as a statement. Now why do you think this would be "much better in terms of efficiency"? – Jon Skeet Jan 30 '18 at 12:18
  • Does `(admin_accounts.add(username))` return `void`? – Magnilex Jan 30 '18 at 12:18
  • are there void methods in there? – Stultuske Jan 30 '18 at 12:18
  • 5
    (I'd also strongly advise you to start following Java naming conventions, as an aside.) – Jon Skeet Jan 30 '18 at 12:18
  • I've added a little more information. I'm unsure whether admin_accounts.add(username) returns void as it is of the ArrayList type and also, an alternative to using a normal array. @ Magnilex @Stultuske – Isaac Odeyale Jan 30 '18 at 12:24
  • @Derlin: This question isn't about Javascript. It's about Java. Hence "Java" in the title and the "java" tag. – Jon Skeet Jan 30 '18 at 12:24
  • aha, oups sorry ^^ didn't read it well. My bad ! – Derlin Jan 30 '18 at 12:25
  • You *still* haven't explained why you think using the conditional operator would be "much better in terms of efficiency". (Hint: it wouldn't be.) – Jon Skeet Jan 30 '18 at 12:25
  • The code *is* inefficient in its use of string concatenation, mind you. It's also really hard to read at the moment with the formatting you've got... – Jon Skeet Jan 30 '18 at 12:26
  • It was an assumption because, I have only learnt a little bit about time-complexity which also includes the line of code for it to do an action. if you could explain to me why it wouldn't be more efficient, I would appreciate it and take it as a learning experience @Jon Skeet – Isaac Odeyale Jan 30 '18 at 12:27
  • @IsaacOdeyale then how would you expect it to work in a ternary operator statement? – Stultuske Jan 30 '18 at 12:27
  • You need to evaluate the condition, and if the result is true, call the `add` method. Which of those steps do you expect would be skipped in the conditional operator version? What unnecessary work do you think would occur in an `if` statement? – Jon Skeet Jan 30 '18 at 12:28

1 Answers1

0

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

;-)

giorgiga
  • 1,758
  • 12
  • 29