Hi I am trying to break from this loop and return the coordinates when both if statements are true. However the loop is never ending. How can I fix it?
public
See if this works. I just made a Boolean flag variable to get out the while loop. It should make the flag false once it reaches the second if.
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:");
Boolean flag = true;
while(flag) {
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)) {
flag = false;
return coordinates;
}
}
System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
}
}