My goal is to print out the given coordinates if they are in the valid format. I have a helper function isValid to check if the string is a coordinate or not, and the positionQuery asks for the users input, if isValid is false positionQuery gives an error and asks for the coordinates again, if it is true it prints out the coordinates. However, there is something wrong about my boolean values or loops because I am not getting any output.
public static boolean validCoordinate(String coordinate, int dimension) {
String [] alphabet = new String[dimension];
for(int i = 0; i < dimension; i++) {
alphabet[i] = Character.toString((char)(65 + i));
}
List<String> list = Arrays.asList(alphabet);
int [] numbers = new int [dimension];
for(int i = 0; i < dimension; i++){
numbers[i] = 1 + i;
}
if(list.contains(Character.toString(coordinate.charAt(0)))) {
for(int j = 0; j < dimension; j++) {
if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
return true;
}
}
}
return false;
}
public static String[] positionQuery(int dim, Scanner test_in) {
Scanner scanner = new Scanner(System.in);
System.out.println("Provide origin and destination coordinates.");
System.out.println("Enter two positions between A1-H8:");
while(true) {
String line = scanner.nextLine();
String[] coordinates = line.split(" ");
if(coordinates.length == 2) {
String origin = coordinates[0];
String dest = coordinates[1];
if(validCoordinate(origin, dim) && validCoordinate(dest,dim)) {
System.out.println(coordinates);
}
}
else {
System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
}
}
}
It would be amazing if I could see what I am doing wrong :)