Issue
I was trying to declare two methods for a java application, both are written with the correct format, but one is valid and the other is marked as an illegal start of expression, so i was wondering what can i do to fix it?
/**
*
* @param valor
* @param matriz
* @return existe
*/
public static boolean verificaExistencia(int valor, int matriz [][]){//this method is marked as an illegal start of expression altough the format is correct
boolean existe=false;
for(int x=0; x<matriz.length;x++){
if(matriz[x][0]==valor){
existe=true;
break;
}
}
return existe;
}
/**
*
* @param valor
* @param array
* @return conta
*/
public static int cuantasVecesExiste(int valor, int array[]){//this method uses the same format as the one above, but is considered valid, what is the issue with the first one?
int conta=0;
for(int x=0;x<array.length;x++){
if(array[x]==valor)
conta++;
}
return conta;
}
}
Solution
Try
public static boolean verificaExistencia(int valor, int matriz[][]) {
for (int x = 0; x < matriz.length; x++) {
if (matriz[x][0] == valor) {
return true;
}
}
return false;
}
Why do you only check the first column in the matrix? If you want to iterate the full matrix you could do somthing like:
public static boolean verificaExistencia(int valor, int matriz[][]) {
for (int row = 0; row < matriz.length; row++) {
for (int col = 0; col < matriz[row].length; col++) {
if (matriz[row][col] == valor) {
return true;
}
}
}
return false;
}
Answered By - corn_not_slapped
Answer Checked By - Katrina (JavaFixing Volunteer)