2

I am trying to validate input from a user.The user puts in Y coordinate a letter between (A-J) 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 (A-J), or Q to QUIT

d

Enter Y Co-Ord (0-9)

h

Please enter a number between 1 -9

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

Enter Y Co-Ord (0-9)

1
  • 1
    You'll want to align your text; one says 0-9, one says 1-9. Commented Jan 18, 2013 at 17:05

2 Answers 2

1

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;
    }
  }
Sign up to request clarification or add additional context in comments.

6 Comments

Edited. I dropped the validInt flag by mistake.
Please, use new syntax for try catch, it will look nicer.
What are you referring to by "validInt flag"? sorry, I'm new to programming.
I was meaning the validInt boolean value that you had inside the catch block in your original code above.
I'm afraid with the limited block of code here I don't see where an infinite look is coming from. I would need to see further code outside this block to get the specific class of input for example.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.