Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: Code not executing (stack implementation)

  1. #1
    mafro is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Unhappy Code not executing (stack implementation)

    Hey I'm new to java and for an assignment I'm to implement a stack using arrays and a GUI.

    Basically the problem I'm facing is when I press a button, a particular line of code isn't executing. The line of code is 'suppose' to take the elements at the top of the stack, and add them together (as shown below).

    Java Code:
    result = ((stack.getElementAt(0)) + (stack.getElementAt(1)));
    This code is located within a JButton event, and result is a class level integer variable (as shown below).

    Java Code:
    private int result = 0;
    and here is the getElementAt code (located in the Stack class):

    Java Code:
    public int getElementAt(int n) {
        return stack[n];
    }
    Java Code:
    if(stack.getStackSize() >= 2) {
        JOptionPane.showMessageDialog(null, "it works!", "Attention", JOptionPane.INFORMATION_MESSAGE);
        result = ((stack.getElementAt(0)) + (stack.getElementAt(1)));
        Display.setText(Integer.toString(result));
    }
    It most likely has a really simple solution but atm I have no idea on why this isn't working.

    Thanks for any help/tips/guidance.
    Last edited by mafro; 03-22-2012 at 04:04 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Code not executing (stack implementation)

    a particular line of code isn't executing.
    Is the line of code inside of a if statement that is not true?
    There is no way to say what the problem is with the information you have given.

    Add lots of println statements to the code to show where it is executing and to show the values of all the variables used. The print out will show you what the code sees and where it is executing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mafro is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Code not executing (stack implementation)

    Quote Originally Posted by Norm View Post
    Is the line of code inside of a if statement that is not true?
    There is no way to say what the problem is with the information you have given.

    Add lots of println statements to the code to show where it is executing and to show the values of all the variables used. The print out will show you what the code sees and where it is executing.
    It is, but the other code within the if is executing. I know this because i used a joptionpane to test whether it was actually reaching the code within the if statement. It appears that any line of code after the joptionpane code isn't functioning so using print statements won't work (i have tried this already).

    Here's the if statement code:

    Java Code:
     if(stack.getStackSize() >= 2) {
        JOptionPane.showMessageDialog(null, "it works!", "Attention", JOptionPane.INFORMATION_MESSAGE);
        result = ((stack.getElementAt(0)) + (stack.getElementAt(1)));
        Display.setText(Integer.toString(result));
    }
    Last edited by mafro; 03-22-2012 at 04:06 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Code not executing (stack implementation)

    Can you post the code you are talking about? Show which lines are executed and which lines are skipped by adding a comment where the execution stops.

    Using println statements are better than joptionpane because you can copy and paste the print out to show what is happening.
    Tolls likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mafro is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Code not executing (stack implementation)

    Quote Originally Posted by Norm View Post
    Can you post the code you are talking about? Show which lines are executed and which lines are skipped by adding a comment where the execution stops.

    Using println statements are better than joptionpane because you can copy and paste the print out to show what is happening.
    if(stack.getStackSize() >= 2) {
    System.out.println("Stack[0] = " + Integer.toString(stack.getElementAt(0)) + ". " + "Stack[1] = " + Integer.toString(stack.getElementAt(1)));
    result = ((stack.getElementAt(0)) + (stack.getElementAt(1)));
    Display.setText(Integer.toString(result));
    }

    The compiler would enter the if statement, execute the joptionpane which then signifies that the if statement is true, and then nothing else would be executed.

    So I replaced the joptionpane code (which was being executed) with a print statement which is not being executed. I'm now starting to think that my Stack.getElementAt method is broken... which is leading to any code containing or following the code with the getElementAt method to be excluded from execution.
    Last edited by mafro; 03-22-2012 at 04:26 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Code not executing (stack implementation)

    excluded from execution.
    That happens when an exception is thrown.
    Do you have a try/catch block surrounding the code?
    Does the catch block ignore any exceptions? Be sure to call printStackTrace() in the catch block.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  2. Help required in executing the following code
    By vivekdarshan in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2011, 12:24 PM
  3. Stack implementation
    By jefrazie in forum New To Java
    Replies: 3
    Last Post: 10-07-2009, 12:59 PM
  4. Help on Stack Implementation
    By danver_2009 in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 08:12 AM
  5. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 11:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •