In: Computer Science
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);
}
One of the way to use try catch in given example is to use User defined Exception
following class can be added for user defined exception :
class ValidInputException extends Exception{
int a;
ValidInputException(int b) {
a=b;
}
public String toString(){
if(a==1){
return ("Exception "+a+" : Input not contains 2 strings") ;
}
else if(a==2){
return ("Exception "+a+" : first String in Input is not in between
A and J") ;
}
else if(a==3){
return ("Exception "+a+" : second string is not a single digit
number ") ;
}
else if(a==4){
return ("Exception "+a+" : The coordinates have already been shot
at ") ;
}
return ("Exception Number = "+a) ;
}
}
Your checkvalidIput(String input) will change accordingly ->
public boolean checkValidInput(String input){
try{
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;
throw new ValidInputException(1);
}
//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;
throw new ValidInputException(2);
//returns false if the second string is not a single digit
number
/*
for (String str: coordinates[1]){
if (numList.contains(str)==false){
return false;
}
}
*/
String str= coordinates[1];
if(str.length()!=1 || numList.contains(str)==false){
throw new ValidInputException(3);// returns false
if the second string is not a single digit number
//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;
throw new ValidInputException(4);
}
}
catch(ValidInputException e){
System.out.println(e);
}
return true;
}