jeudi 29 décembre 2016

Is this error selection or conditional? (Java)

I have an error, on the third IF statement. It checks to see if the user entered at number within a range (1-6) and the letter is a-f. I cant test my search algorithm because there seems to be an error in that line. I can't work it out. What seems to be wrong? Is it the answer.charAt(1)?

boolean wronganswer = true;
while (wronganswer == true){

    answer = (String)JOptionPane.showInputDialog(null, new JLabel(sb.toString()), "Battleships", JOptionPane.INFORMATION_MESSAGE, pic, null, "");
    if(answer.length() == 2){
        if ((Character.isLetter(answer.charAt(0))) && (Character.isDigit(answer.charAt(1)))){
            if ((answer.charAt(1) >= 0) && (answer.charAt(1) <= 6)){
                for (int k = 0; k < rows.length; k++){
                    if(rows[k] == (""+answer.charAt(0))){
                        wronganswer=false;
                    }
                }
                JOptionPane.showMessageDialog(null,"No! That letter is not on the grid!");
            }
            else{
                JOptionPane.showMessageDialog(null,"No! That number is not on the grid!");
                System.out.println(answer.charAt(1));
            }
        }
        else{
            JOptionPane.showMessageDialog(null,"No! Enter a letter, THEN a number!");
        }
    }
    else{
        JOptionPane.showMessageDialog(null,"No! Enter ONE letter and ONE number!");
    }

}

[EDIT] Fixed an indentation error when pasting to stack overflow

Aucun commentaire:

Enregistrer un commentaire