1

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)
4
  • What's your question? Commented Jun 2, 2014 at 5:06
  • Well, problem = false is just wrong, but that's not really what's breaking you. It's still worth fixing (and should be simplified to !problem). Commented Jun 2, 2014 at 5:09
  • Your Stack is empty at the time you are calling [peek()](docs.oracle.com/javase/7/docs/api/java/util/Stack.html#peek()) Commented Jun 2, 2014 at 5:10
  • Did you try running your code through a debugger. That would probably have shown you what's wrong with it... Commented Jun 2, 2014 at 5:26

2 Answers 2

4

As per the API documentation, peek() method will throw EmptyStackException, if the stack is empty.

You have to check whether the stack is empty or not before peek a value from it. There is a empty() method which return a boolean depends on the stack

Removes the object at the top of this stack and returns that object as the value of this function.

Returns:

The object at the top of this stack (the last item of the Vector object).

Throws:

EmptyStackException - if this stack is empty.

Sign up to request clarification or add additional context in comments.

Comments

0

One possible cause could be that there is an if condition saying if(problem = false){ (actually it should be problem==false or even better !problem). The output of the statement problem = false will always be false hence the condition fails every time and nothing is added to the Stack (except for s.push(0) in the beginning). Now you are trying to pull (peek()) something from the Stack and the Stack is empty so you are getting the exception. Also it is best practice to always check if the stack is empty before pulling something from it. You can use Stack.isEmpty() for that purpose. I have not tested, but problem = false is definitely wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.