I need help with my PuzzleSolver class. I get a notice saying
Note: SliderPuzzleSolver.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
I don't understand what this means and also when I run my tester I get this
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at SPS_Tester.main(SPS_Tester.java:23)
If anyone would have some guesses about any of these please shoot them my way.
public class SliderPuzzleSolver {
/* Returns a String encoding a shortest sequence of moves leading from
** the given starting configuration to the standard goal configuration,
** or null if there is no such sequence.
** pre: !start.equals( the standard goal configuration )
*/
public static String solution(SliderPuzzleConfig start) {
int numRows = start.numRows();
int numCols = start.numColumns();
SliderPuzzleConfig standardGoal = new SliderPuzzleConfig(numRows, numCols);
return solution(start, standardGoal);
}
public static String solution(SliderPuzzleConfig start,
SliderPuzzleConfig goal) {
SliderPuzzleSolverNode treeNode = new SliderPuzzleSolverNode(start, "");
QueueViaLink1<SliderPuzzleSolverNode> toBeExplored = new QueueViaLink1();
toBeExplored.enqueue(treeNode);
while (!toBeExplored.isEmpty()) {
treeNode = toBeExplored.dequeue();
SliderPuzzleConfig config = treeNode.config;
char[] direction = { 'U', 'D', 'L', 'R' };
for(int i = 0; i <= 4; i++) {
if(config.canMove(direction[i])) {
SliderPuzzleConfig newConfig = config.clone();
newConfig.move(direction[i]);
if(newConfig != goal) { //newConfig was not discovered earlier
String moveSeq = treeNode.moveSeq + direction;
SliderPuzzleSolverNode newTreeNode = new SliderPuzzleSolverNode(newConfig, moveSeq);
toBeExplored.enqueue(newTreeNode);
if (newConfig == goal) {
return moveSeq;
}
}
}
}
}
return null;
}
}
public class SPS_Tester {
/* Provided with command line arguments (or what jGrasp refers to as
** "run arguments") indicating the dimensions of a puzzle, a seed
** for a pseudo-random number generator, and a number of pseudo-random
** moves to make --starting with the standard goal configuration-- to
** obtain an initial configuration, this method uses the solution()
** method in the SliderPuzzleSolver class to find a minimum-length
** sequence of moves that transforms that initial configuration into
** the standard goal configuration. This method then displays the
** configurations along that path of moves.
*/
public static void main(String[] args) {
int numRows = Integer.parseInt(args[0]);
int numCols = Integer.parseInt(args[1]);
int seed = Integer.parseInt(args[2]);
Random rand = new Random(seed);
int numMoves = Integer.parseInt(args[3]);
SliderPuzzleConfig start =
new SliderPuzzleConfig(numRows, numCols, rand, numMoves);
System.out.println("Starting configuration is");
start.display();
System.out.println();
String solution = SliderPuzzleSolver.solution(start);
if (solution == null) {
System.out.println("No solution was found.");
}
else {
System.out.printf("Path of length %d found:\n\n", solution.length());
displayPath(start, solution);
}
System.out.println("\nGoodbye.");
}
/* Displays the sequence of configurations starting with the one
** given and proceeding according to the given sequence of moves.
*/
public static void displayPath(SliderPuzzleConfig config, String moveSeq) {
SliderPuzzleConfig spc = config.clone();
spc.display();
for (int i = 0; i != moveSeq.length(); i++) {
char dir = moveSeq.charAt(i);
spc.move(dir);
System.out.printf("\nAfter moving %c:\n", dir);
spc.display();
}
}
}
Note: SliderPuzzleSolver.java uses unchecked or unsafe operations
It means you are not using a generic type somewhere that you could or should. This is just a warning. It looks like the only place is
QueueViaLink1<SliderPuzzleSolverNode> toBeExplored = new QueueViaLink1();
It should be new QueViaLink1<SliderPuzzleSolverNode>()
or in recent versions of Java you can also use newQueViaLink1<>()
. You can use the compiler settings in jGRASP, or whatever IDE you're using if it's not that, to add the suggested compiler flag if you want the specific error messages.
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
It appears you are not passing any command line arguments. Your program should test the length of args[] before accessing the elements, and fail with an error message if there are not enough.