Home > other >  error: bad operand types for binary operator '!=' if(Current_value != "."){ ^ fi
error: bad operand types for binary operator '!=' if(Current_value != "."){ ^ fi

Time:01-10

class Solution {
    public boolean isValidSudoku(char[][] board) {
        HashSet<String> seen= new HashSet();
        for(int i =0;i<9;i  ){
            for(int j=0;j<9;j  ){
                char Current_value = board[i][j];
                if(Current_value != "."){
                    if(!seen.add(Current_value  "found in row" i) || !seen.add(Current_value "found in column" j) || !seen.add(Current_value  "found in sub box" i/3  j/3))
                        return false;
                }
            }
        }
        return true;
        
        
    }
}

why this error is shown Line 7: error: bad operand types for binary operator '!=' if(Current_value != "."){ ^ first type: char second type: String

CodePudding user response:

There is difference in "." and '.'. The former is a string and the latter one is a character. The compile time error you are sharing is because the relationship operator can compare values of identicial data types. To remove this error, change it to the following.

class Solution {
public boolean isValidSudoku(char[][] board) {
    HashSet<String> seen= new HashSet();
    for(int i =0;i<9;i  ){
        for(int j=0;j<9;j  ){
            char Current_value = board[i][j];
            if(Current_value != '.'){
                if(!seen.add(Current_value  "found in row" i) || !seen.add(Current_value "found in column" j) || !seen.add(Current_value  "found in sub box" i/3  j/3));
                    return false;
            }
        }
    }
    return true;
    
    
}

}

CodePudding user response:

You are comparing String with char value.

char Current_value = board[i][j];
if(Current_value != '.'){
   if(!seen.add(Current_value  "found in row" i) || !seen.add(Current_value "found in column" j) 
        || !seen.add(Current_value  "found in sub box" i/3  j/3));
   return false;
}

CodePudding user response:

Replace your if condition with this. As "." is a string

if(Current_value != '.'){
  •  Tags:  
  • Related