-1

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 :)

Kale Joe
  • 163
  • 6
  • 1
    that's because the `if(validCoordinate(origin, dim) && validCoordinate(dest,dim))` is always false – Kurohige Feb 16 '20 at 22:00
  • Does this answer your question? [How to break this while(true) loop?](https://stackoverflow.com/questions/60251495/how-to-break-this-whiletrue-loop) – PiRocks Feb 16 '20 at 22:17

1 Answers1

1

Your method is named validCoordinate (not isValid). And, that is far too much code for that method. Check that there are two characters, check that both are in their respective ranges. No loops, no collections, no arrays. Like,

public static boolean validCoordinate(String coordinate, int dim) {
    if (coordinate.length() == 2) {
        char a = coordinate.charAt(0);
        char b = coordinate.charAt(1);
        return a >= 'A' && a <= ('A' + dim) && b >= '1' && b <= ('1' + dim);
    }
    return false;
}

And, System.out.println(coordinates); is not how you print an array. Try

System.out.println(Arrays.toString(coordinates));

and please stop asking the same question. And read How to create a Minimal, Reproducible Example.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249