for(int n = 0; n < matchPosition.length; n++){ for(int m = 0; m < 8; m++) { if(matchPosition[n + 1] == relationshipGrid[matchPosition[n]][m]){ tempWord2[n] = tempWord[n]; if((n == tempWord.length - 1) && (tempWord2[tempWord2.length - 1] != tempWord[tempWord.length - 1])){ next = 0; break; } if(tempWord2[tempWord2.length - 1] == tempWord[tempWord.length - 1]){ System.out.println(tempWord2); next = 0; break; } } } }