Why is the while loop never ending?

拥有回忆 提交于 2020-02-26 04:00:09

问题


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.");
        }
    }

来源:https://stackoverflow.com/questions/60251059/why-is-the-while-loop-never-ending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!