2

I am getting this error when I run the following code and enter 5x5 for size. Not sure why?

When I enter 10x10 it seems to run ok but I'm not sure if the output is correct.

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)"

Here is my code

    import java.util.Scanner;

    public class CheckerBoard {

        public static void main(String [] args){

        Scanner userInput = new Scanner(System.in);

        System.out.println("What two colors would you like your board to be?");

        String colorOne = userInput.next();
        String colorTwo = userInput.next();

        do {    
            System.out.println("How big should the checker board be? (Square sizes only please)" + "\n"
                + "Please enter it like 4x4 with whatever numbers you choose.");

            String boardSize = userInput.next();

            int intOne = Integer.parseInt(boardSize.substring(0,boardSize.indexOf("x")));
            System.out.println(boardSize.indexOf("x"));
            int intTwo = Integer.parseInt(boardSize.substring(boardSize.indexOf("x")+1, boardSize.length()-1));
            System.out.println(intOne);

        } while(false);
        }
    }

    //keep in mind that this program is not done yet, this is just a current issue I am having atm.
1
  • It would help if you could specify the line where the exception comes from. For me it seems you are using wrong indices for at least one of the substring method calls. Commented Feb 17, 2014 at 20:19

5 Answers 5

2

I believe a safer way to do it will be to split on x

String boardSize = userInput.next();
String[] split = boardSize.split("x");
int intOne = Integer.parseInt(split[0]);
int intTwo = Integer.parseInt(split[1]);

Obviously sanitize for BAD INPUT!

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is here:

int intTwo = Integer.parseInt(boardSize.substring(boardSize.indexOf("x")+1, boardSize.length()-1));

You're taking the substring from x to length - 1. You should be going from x to length because substring does not include the second index.

So, you were receiving an error on 5x5 because there is only one character after the x. So you were trying to parseInt an empty string. You didn't have the exception on 10x10, but you were only using 10x1.

Thus, you should change the line to this:

int intTwo = Integer.parseInt(boardSize.substring(boardSize.indexOf("x")+1, boardSize.length()));

Comments

0

Your code does not take into consideration that the boardSize string might actually be empty, when running the line;

int intOne = Integer.parseInt(boardSize.substring(0,boardSize.indexOf("x")));

When you do "indexOf", searching for something that does not exists -- you'll get -1 back, which is invalid as argument to your substring.

Comments

0

Have you tried this?

        int intTwo = Integer.parseInt(boardSize.substring(boardSize.indexOf("x")+1, boardSize.length()));

Probably the longer you look will solve the parsing problem.

Comments

0

Change
int intTwo = Integer.parseInt(boardSize.substring(boardSize.indexOf("x")+1, boardSize.length()-1));
to
int intTwo = Integer.parseInt(boardSize.substring(boardSize.indexOf("x")+1, boardSize.length()));
Remember, the second index passed to String.substring will not be included in the return value.

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.