1. Engineering
  2. Computer Science
  3. this is a custom exception handling class for a battleship...

Question: this is a custom exception handling class for a battleship...

Question details
This is a custom exception handling class for a Battleship game. I have added comments for how I understand the code, line by line. However, I don't understand it fully yet. Please modify/add to my existing comments if it is wrong or not complete. 


public boolean checkValidInput(String input){

    try{
        // Why declare this ArrayList?
        ArrayList<String> numList = new ArrayList<String>();
        for (int i = 0; i < 10; i++){
            // what is being added?
            numList.add("" + i);
        }
        // Declare a 1D array of type String to store coordinates to fire at, separated by a space.
        String[] coordinates = input.split(" ");

        // If two strings were not entered
        if (coordinates.length != 2){
            throw new ValidInputException(1);
        }

        // If the row input was not entered in range 'A' to 'J', return false.
        else if(coordinates[0].charAt(0) < 'A'|| coordinates[0].charAt(0) < 'J'){
         
            throw new ValidInputException(2);
        }

        // Declare variable of type String and set it equal to coordinates for column input.
        String str = coordinates[1];

        // If the second input is not error single integer or it is not in the list, return false.
        if(str.length() != 1 || !numList.contains(str)){
            // Return false
            throw new ValidInputException(3);
        }
        // Declare a row and column variable of type int and parse the input.
        int row = Integer.parseInt(coordinates[0]);
        int column = Integer.parseInt(coordinates[1]);

        // If the coordinates have been shot at already, return false.
        if (!this.availableSpot[row][column]){
           
            throw new ValidInputException(4);
        }

    }
    // Catch all exceptions
    catch(ValidInputException e){
        // does this print the error message?
        System.out.println(e);
    }
    // not sure what it is returning?
    return true;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution