1

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 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)) {
                    return coordinates;
                }
            }
            System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
        }
    }

I guess I have a problem with validCoordinates, because I am only getting true with validCoordinates, but couldn't find what I am doing wrong.

public static boolean validCoordinate(String coordinate, int dimension) {
        boolean isValidCoordinate;
        String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        int [] numbers = new int [dimension];
        int one = 1;
        for(int i = 0; i < dimension; i++){
            numbers[i] = one + i;
        }
        for(int i = 0; i < dimension; i++){
            if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
                for(int j = 0; j < dimension; j++) {
                    if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
Kale Joe
  • 163
  • 6
  • 1
    Try printing `origin` and `dest`, and make sure that they are what you expect. return should break out of the loop as you expect. – PiRocks Feb 16 '20 at 18:08
  • I am pretty sure that your ```return``` statement will break the loop. But maybe ```validCoordinate()``` does not work as expected. – Stefan Feb 16 '20 at 18:09
  • Well now it's an infinite loop, [`validCoordinate()`](https://stackoverflow.com/questions/60251059/why-is-the-while-loop-never-ending) needs [debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Elliott Frisch Feb 16 '20 at 18:15
  • System.out statement at the bottom is unreachable btw. – H.Ç.T Feb 16 '20 at 18:22
  • Does this answer your question? [Why is the while loop never ending?](https://stackoverflow.com/questions/60251059/why-is-the-while-loop-never-ending) – PiRocks Feb 16 '20 at 22:17

1 Answers1

2

Your validcoordinate is incorrect, it only checks the first few values specified by the dimension argument in the alphabet array.

Try this

public static boolean validCoordinate(String coordinate, int dimension) {
        boolean isValidCoordinate;
        String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        int [] numbers = new int [dimension];
        for(int i = 0; i < dimension; i++){
            numbers[i] = 1 + i;
        }
        if(Arrays.asList(alphabet).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;
    }

you can definitely improve this code and I will leave that as a challenge to you.

here is the associated repl https://repl.it/@Blakeinstein/question60251495

ninja edit: I suggest you to modify your positionquery

public static String[] positionQuery(int dim) {
        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)) {
                    return coordinates;
                }
                else{
                  System.out.println("Coordinates are not valid");
                }
            }
            else{
              System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
            }
        }
    }
Blaine
  • 576
  • 9
  • 30