while i am running the below source code
import java.util.Stack;
public class Assignment3 {
boolean conflict, complete = false;
public static int solve(int n) {
int solution = 0;
int nextQueen = 0;
boolean problem = false;
s.push(0);
do{
for(int i = 0; i < s.size(); i++)
{
if(s.get(i) == nextQueen){
problem = true;
break;
}
else if(s.get(i) - i == nextQueen - s.size()){
problem = true;
break;
}
else if(s.get(i) + i == nextQueen + s.size()){
problem = true;
break;
}
}
if(problem = false){
s.push(nextQueen);
nextQueen = 0;}
else{
nextQueen++;
}
if(nextQueen == n){
if(s.peek() == n){
s.pop();
nextQueen = s.pop()+ 1;
}
else{
nextQueen = s.pop()+ 1;
}
}
}while(s.size() != n);
printSolution(s);
solution++;
return solution;
}
private static void printSolution(Stack<Integer> s) {
for (int i = 0; i < s.size(); i ++) {
for (int j = 0; j < s.size(); j ++) {
if (j == s.get(i))
System.out.print("Q ");
else
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
// ----- the main method -----
// (you shouldn't need to change this method)
public static void main(String[] args) {
int n = 8;
// pass in parameter n from command line
if (args.length == 1) {
n = Integer.parseInt(args[0].trim());
if (n < 1) {
System.out.println("Incorrect parameter");
System.exit(-1);
}//if
}//if
int number = solve(n);
System.out.println("There are " + number + " solutions to the " + n + "-queens problem.");
}
}
Getting the following error. Help me to clear :
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:79)
at Assignment3.solve(Assignment3.java:40)
at Assignment3.main(Assignment3.java:87)
problem = falseis just wrong, but that's not really what's breaking you. It's still worth fixing (and should be simplified to!problem).Stackis empty at the time you are calling[peek()](docs.oracle.com/javase/7/docs/api/java/util/Stack.html#peek())