简体   繁体   中英

How to break this while(true) loop?

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;
    }

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;
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM