Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details
Below are three methods: checkValidInput, getCoordinates and play. Looking at checkValidInput, I feel it is very longwinded and would like to modify by using a try/catch block for user input. How is this occomplised? Would I be able to get rid of checkValidInput method with a try/catch? Any code to help me out would be great.

public boolean checkValidInput(String input){
    ArrayList<String> numList = new ArrayList<String>();
    for (int i=0;i<10;i++){
        numList.add(""+i);
    }
    String[] coordinates = input.split(" ");
    //returns false if there are not 2 strings
    if (coordinates.length!=2){
        return false;
    }
    //returns false if first is String is not in between A and J
    if(coordinates[0].charAt(0)<'A' || coordinates[0].charAt(0)>'J')
        return false;
    //returns false if the second string is not a single digit number
    for (String str: coordinates[1]){
        if (numList.contains(str)==false){
            return false;
        }
    }
    //returns false if the coordinates have already been shot at
    int row = Integer.parseInt(coordinates[0]);
    int column = Integer.parseInt(coordinates[1]);
    if (this.availableSpot[row][column]==false){
        return false;
    }

    return true;
}
public int[] getCoordinates(String input)
{
    int[] coordinates = new int[2];
    String[] strList = input.split(" ");
    int row = (int) strList[0].charAt(0) - 65;
    int column = Integer.parseInt(strList[1]);
    coordinates[0] = row;
    coordinates[1] = column;
    return coordinates;
}
public void play()
{
    print(101);
    print(1);
    ocean.randomShipDeployment();
    boolean isGameOver = ocean.isGameOver();
    sc = new Scanner(System.in);

    //printOcean the ocean and start the game
    ocean.printOcean();
    print(3);
    while (!isGameOver)
    {
        print(2);
        String input = sc.nextLine();

        //check if input is valid
        while (!checkValidInput(input))
        {
            print(99);
            input = sc.nextLine();
        }

        //get coordinates and fire
        int[] coordinates = getCoordinates(input);
        int row = coordinates[0];
        int column = coordinates[1];
        ocean.shotFiredAtLocation(row, column);
        availableSpot[row][column] = false;
        isGameOver = ocean.isGameOver();
        ocean.printOcean();
        print(3);
        print(100);
    }
    //printOcean info saying you win
    print(4);
    print(5);
}
Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.