简体   繁体   中英

Java error for NumberFormatException

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.

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!

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()));

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.

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.

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.

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