Java error for NumberFormatException -
I am getting this error when I run the following code and enter 5x5 for size. Sure why
When I enter 10x10 then it's going fine, but I'm not sure the output is correct. Exception in the thread "main" Java Lang.NumberFormatException: for input string: java.lang.NumberFormatException.forInputString (unknown source) at java.lang.Integer.parseInt (unknown source)
This is my code on java.lang.Integer.parseInt (unknown source)
import java.util.Scanner; public class checkerboard {public Static zero main (string [] args) {scanner userInput = new scanner (System.inout.println ("Do you prefer two colors to your board?"); String color-on = user input.net;); String colortivo = user input.net (); {System out. Println ("How big should be a checker board? (Square size only please)" + "\ n" + "Please select the number, like 4x4 Write string. "); String boards seizures = user ipu t.next (); int intOne = integer paraset (board size. Substring (0, board size indexf (" x ")); println (boardSize.indexOf (" x ")); Int intTwo = integer paraset (board size. Substring (boardSize.indexOf (" x ") + 1, board seismel. Long () - 1)); Println (loud); } While (false); }} / Keep in mind that this program has not yet been done, it is only a current issue that I am making an ATM.
The problem is here:
int intTwo = integer Parasent (board size. Substring (boardSize.indexOf ("x") + 1, board seizel. Length () - 1));
You are taking substruments from x
to length - 1
. You should go from x
to length
because because the second index is not included in substring
.
So, you were getting an error on 5x5
because X is only one letter after you are trying to empty the string parseInt
Were. You did not have an exception at 10x10
, but you were only using 10x1
.
In this way, you should change this line to:
int intTwo = integer ParseInt (boardSize.substring (boardSize.indexOf ("x") + 1, boardism. Length ()));
Comments
Post a Comment