Results 1 to 6 of 6
- 03-22-2012, 02:29 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
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).
This code is located within a JButton event, and result is a class level integer variable (as shown below).Java Code:result = ((stack.getElementAt(0)) + (stack.getElementAt(1)));
and here is the getElementAt code (located in the Stack class):Java Code:private int result = 0;
Java Code:public int getElementAt(int n) { return stack[n]; }It most likely has a really simple solution but atm I have no idea on why this isn't working.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)); }
Thanks for any help/tips/guidance.Last edited by mafro; 03-22-2012 at 04:04 PM.
- 03-22-2012, 03:38 PM #2
Re: Code not executing (stack implementation)
Is the line of code inside of a if statement that is not true?a particular line of code isn't executing.
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.
- 03-22-2012, 04:03 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Code not executing (stack implementation)
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.
- 03-22-2012, 04:09 PM #4
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.If you don't understand my response, don't ignore it, ask a question.
- 03-22-2012, 04:20 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Code not executing (stack implementation)
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.
- 03-22-2012, 04:42 PM #6
Re: Code not executing (stack implementation)
That happens when an exception is thrown.excluded from execution.
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
-
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
Help required in executing the following code
By vivekdarshan in forum Advanced JavaReplies: 6Last Post: 01-24-2011, 12:24 PM -
Stack implementation
By jefrazie in forum New To JavaReplies: 3Last Post: 10-07-2009, 12:59 PM -
Help on Stack Implementation
By danver_2009 in forum New To JavaReplies: 1Last Post: 02-16-2009, 08:12 AM -
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12-19-2008, 11:58 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks