简体   繁体   中英

Java integer input validation

I am trying to validate input from a user.The user puts in Y coordinate a letter between (AJ) and x coordinate a number between (1-9).I can validate the y coordinate but am having trouble validating the x coordinate. I want it so if the user puts in something other than a number between 1 and 9 it keeps asking the user for valid input.

    do {
        // inner loop checks and validates user input
        do {

            System.out.println("Enter X Co-Ord (A-J), or Q to QUIT");
            letter = input.next().toUpperCase(); // upper case this for
                                                    // comparison
            if (letter.equals("Q"))
                break; // if user enters Q then quit

            String temp = "ABCDEFGHIJ";

            while (temp.indexOf(letter) == -1) {
                validString = false;
                System.out.println("Please enter a valid input");
                letter = input.next().toUpperCase();
                col = temp.indexOf(letter);

            }

            if (temp.indexOf(letter) != -1) {
                validString = true;
                col = temp.indexOf(letter);

            }
            try {

                System.out.println("Enter Y Co-Ord (0-9)");
                row = input.nextInt();


            } catch (InputMismatchException exception) {
                validInt = false;
                System.out.println("Please enter a number between 1 -9");
            }

            catch (Exception exception) {
                exception.printStackTrace();
            }

            valuesOK = false; // only set valuesOK when the two others are
                                // true
            if (validString && validInt) {
                valuesOK = true;
            }
        } while (!valuesOK); // end inner Do loop

The output is:

Enter X Co-Ord (AJ), or Q to QUIT

d

Enter Y Co-Ord (0-9)

h

Please enter a number between 1 -9

Enter X Co-Ord (AJ), or Q to QUIT

Enter Y Co-Ord (0-9)

You just need to put a while loop around your nextInt() the same as you do when reading the letter:

  System.out.println("Enter Y Co-Ord (0-9)");
  row = -1
  while (row < 0) {
    try {
      row = input.nextInt();
      validInt = true;
    } catch (InputMismatchException exception) {
      System.out.println("Please enter a number between 1 -9");
      row = -1;
      validInt = false;
    }
  }

Just want to make the validation make more sense with the requirement, since human 's eye can skip easily the line nextInt()

String value = "";
System.out.println("Enter Y Co-Ord (1-9)");

while (!(value = input.next()).matches("[1-9]+")) {
    System.out.print("Wrong input. Insert again: ");
}

System.out.println(value);

Of course, when you get the right value, then you can parse it to integer again (safely !!!)

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