Hello Name
Hello Name

Reputation: 37

Why can't I have a 'void' type here?

For some reason, I am getting this error: error: 'void' type not allowed here.

This is in the public static void main(String[] args){}"method".

if(Arrays.equals(Arrays.sort(arrayCorrectLength1), arrayCorrectLength1) == false){
      System.out.println("Error.");
}

I have java.util.Arrays imported already. The program is supposed to take to string arrays and merge them, but first I need to check if they are in alphabetical order, so I check if the sorted version of the array is = to the original array. This is what I get.

Upvotes: 1

Views: 78

Answers (1)

Ousmane D.
Ousmane D.

Reputation: 56423

Arrays.sort returns void hence it cannot be passed as an argument to Arrays.equals.

rather create a stream from the array, sort it, collect it into an array and then pass that as input to Arrays.equals.

if(!Arrays.equals(Arrays.stream(arrayCorrectLength1)
          .sorted().toArray(String[]::new), arrayCorrectLength1)){
         System.out.println("Error.");
}

Upvotes: 4

Related Questions