I make chessboard, where alone king will walk according to the rules of chess. Below it's method to make, it called for 2 times for i and j coordinate of King, I Made input variable String to check if this King's coordinates already exist. Than I try to convert it to integer, seems something wrong with this conversion.
import java.util.*;
public class King {
int move(String iK){
Random rand = new Random();
Integer coordinateKing = Integer.valueOf(iK);
if (iK == null){
coordinateKing = rand.nextInt(8);
}else{
int caseI;
switch(caseI = rand.nextInt(2)){
case 0: if (coordinateKing < 8){ coordinateKing++; } else {caseI = rand.nextInt(2);}
break;
case 1: if (coordinateKing > 0){ coordinateKing--; } else {caseI = rand.nextInt(2);}
break;
default:
break;
}
}
return coordinateKing;
}
}
I have problem like this:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.valueOf(Integer.java:582)
at chess_engine.King.move(King.java:6)
at chess_engine.MainClass.main(MainClass.java:12)
Thanks in advance!
iK?iKisnulland hence you can't parse it. May you want to just declarecoordinateKingand movecoordinateKing = Integer.valueOf(iK);in the else block ?