0

I am trying to ask scanner input until I get a valid answer but my while loop is not ending. First I am asking for user input, and then if the input is valid I am printing out the coordinates and if it is not valid I am throwing an error and asking for input again. This is my code:

public static String[] positionQuery(int dim, Scanner test_in) {
        String[] coordinates;
        Scanner stdin = new Scanner(System.in);
        System.out.println("Provide origin and destination coordinates.");
        System.out.println("Enter two positions between A1-H8:");
        String s1 = stdin.nextLine();
        coordinates = s1.split(" ");
        String origin = coordinates[0];
        String dest = coordinates[1];

        while (!validCoordinate(origin,dim) || !validCoordinate(dest,dim) || coordinates.length != 2) {
            System.out.println("ERROR: Please enter valid coordinate pair separated by space.");
            Scanner stdin2 = new Scanner(System.in);
            System.out.println("Provide origin and destination coordinates.");
            System.out.println("Enter two positions between A1-H8:");
            String s2 = stdin.nextLine();
            coordinates = s1.split(" ");
            String origin2 = coordinates[0];
            String dest2 = coordinates[1];
        }
        return coordinates;

    }

How can I fix it? I also tried a do while loop but still the loop is not terminating:

public static boolean validCoordinate(String coordinate, int dimension) {
    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;
    }
    String[] validC = new String [dimension]; //if the first char  is a letter + a number that is determined my the dimension (string concat
    for(int i = 0; i < dimension; i++) {
        for(int j = 0; j < dimension; j++){
            validC [i] = alphabet[j] + Integer.toString(numbers[j]);
        }
    }

    for(int i = 0; i < dimension; i ++) {
        if(validC[i].equals(coordinate)) {
            return true;
        }
        else {
            return false;
        }
    }
        return true;
}

This is the final code but still not ending :/

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.");
        }
    }
Kale Joe
  • 163
  • 6
  • 6
    You never modify `origin`, `dim` or `dest` in your loop over `origin`, `dim` **and** `dest`. `!validCoordinate(origin,dim) || !validCoordinate(dest,dim)` – Elliott Frisch Feb 16 '20 at 17:16
  • You call the method ```positionQuery``` with a Scanner as argument and inside the method you create more Scanner instances. This is wrong, you should only have one single scanner at a time connected to System.in, so use the one that you passed as function argument. – Stefan Feb 16 '20 at 17:34
  • The scanner inputs should be inside the loop right? – Kale Joe Feb 16 '20 at 17:39
  • Does this answer your question? [How to break this while(true) loop?](https://stackoverflow.com/questions/60251495/how-to-break-this-whiletrue-loop) – Blaine Feb 17 '20 at 05:49

0 Answers0