0

I'm making a basic game of Tic Tac Toe, accepting player input in the form of a string (i.e. a2). The first char is made into an int called row depending on the letter, the same being said for the second char into col (for array grid[row][col]). I have a block of code that throws a custom exception in the event that the first char isn't a, b, or c, and if the second char isn't 1, 2, or 3:

if(input == null) {
        throw new NullInputException();
    }
    else if(input.length() != 2) {
        throw new InvalidInputException();
    }
    else if(!(input.substring(0,1).equalsIgnoreCase("a") &&
            input.substring(0,1).equalsIgnoreCase("b") &&
            input.substring(0,1).equalsIgnoreCase("c") ||
            input.substring(1).equals("1") &&
            input.substring(1).equals("2") &&
            input.substring(1).equals("3"))) {
        throw new InvalidInputException();
    }

The problem is, this code throws an error even when the input is valid, and I don't know why. I've tried using .charAt() as opposed to .substring(), as well as messed around with my conditional statements. My question is: How do I fix this so that it accepts valid input?

Other questions that just don't help: fill two dimensional array with parts of a string; fill a 2d array with chars of 2 string

2
  • You are not using && and || properly. Also, clarify what this has to do with 2 dimensional arrays. Commented Mar 19, 2015 at 15:46
  • According to your code, the only valid input is a string that contains "abc123". Which means any two character input is immediately invalid because it necessarily is missing at least 4 of the other required characters. Commented Mar 19, 2015 at 15:57

2 Answers 2

3

Sometimes it is better to write a series of simpler tests which are easier to read and verify

row = input.substring(0,1).toUpperCase();
col = input.substring(1);
boolean validRow = (row.equals("A") ||
                    row.equals("B") ||
                    row.equals("C"));
boolean validCol =
        (col.equals("1") ||
        col.equals("2") ||
        col.equals("3"));

if(!(validRow && validCol)) {
Sign up to request clarification or add additional context in comments.

Comments

1

You AND two conditions:

input.substring(0,1).equalsIgnoreCase("a") &&
            input.substring(0,1).equalsIgnoreCase("b")

Both cannot be true in the same time. That is why the result is always false and an exception is thrown.

What you really want is:

String first = input.substring(0,1);
String second = input.substring(1);
    if (!((first.equalsIgnoreCase("a") ||
                first.equalsIgnoreCase("b") ||
                first.equalsIgnoreCase("c")) && 
                (second.equals("1") ||
                second.equals("2") ||
                second.equals("3"))) {
        throw new InvalidInputException();
    }

Small edit for Neil...

4 Comments

But with this, to throw the exception, wouldn't the input have to be bad on both parts (i.e. d4)? If the user simply mis-inputted for one char (i.e. c4) then would this catch the error?
@Alex This only allows a1, a2, a3, b1, b2, b3, c1, c2, c3. If the input is not one of those, then it is invalid.
My bad. It worked perfectly. I suppose I was just having an issue with my logic. As for the downvoter, care to explain? The solution was perfect.
It seems that I am the down-voter, but I have absolutely no idea how it happened. I am happy to upvote to rectify this, but the site requires @vojta to edit his answer first since some time has passed since this happened.

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.