![]() |
| ||
| i need help on stack. If pop: stack is empty. hello.. i really need help.. I don't know why i keep on getting an empty stack if i pop a token from stack even if user has already push a token into it.. public class MStack implements Stack{this is another class. This has the main type: import java.io.*; It implements the Stack interface. I run this on eclipse compiler. Can someone help me please.. thanks a lot.. i really need to finish this asap.. |
| ||
| Re: i need help on stack. If pop: stack is empty. hello! may i know wat T[] and oldT[] represent? how do u derive them? |
| ||
| Re: i need help on stack. If pop: stack is empty. Can you post the error you are getting? It should include the line number, method names, and error name... info that would be incredibly helpful in helping you. |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
when you put an item in the shelf, is done by push method, and then try to retreive the item, done by pop, i doesn't have error, only just print's stack empty.. because i did specify in the code that if empty print to console Satck is empty. the program continues to run. ok w8, i got this recenlty Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at MStack.push(MStack.java:45) at BookShelf.options(BookShelf.java:48) at BookShelf.<init>(BookShelf.java:24) at BookShelf.main(BookShelf.java:19) |
| ||
| Re: i need help on stack. If pop: stack is empty. Those T[], B[], and oldT[], keeps track of which stack you want, either stack1, stack2, and stack3. If i chose stack1 to put an item, it pushes and is ok,but if i try to pop the item in which i put in stack1, it says stack empty. If i try to view what is inside the stack 1 2 3, i get stack 1 = the item i just pushed, stack 2 = the item i just pushed, and stack 3 = the item i just pushed, which is wrong because i only pushed an item in stack 1. |
| ||
| Re: i need help on stack. If pop: stack is empty. kung pwde k mgbisaya ok lng, pasabta lng ko sa T[] ug OldT[] ug nganong k2ng sa Fig 1.13 (page 35) sa JEDI course notes dili masabtan :( |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. Your code in "push" takes two arguments: An integer representing the index you're "pushing" the item onto, and the item that you want to push onto the stack. You got the Exception because you tried to push an item onto the stack at index = 3, but the array is too small; therefore, you can't push anything onto index 3 since it doesn't have 3 indexes. Why do you need to give the 'push' method an index, anyway? Instead, you could have a variable 'currentSize' in the MStack class that keeps track of how large your stack is. Then, in your push() method, you'd just automatically add the new item onto the end and increment the 'currentSize' variable. A possible problem: Since you are using an array, and not an Arraylist, you need to make sure that you allocate enough space (make the array big enough) to begin with. Otherwise, at some point, you might get an ArrayIndexOutOfBoundsException. If your array reaches it's maximum size (or at some point before it reaches its max size), you will have to make a new array which has a bigger size and copy the old one over to it. So somehow you're either not making your array big enough or you're calculating the wrong index (that you pass to the push method). |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. So you have multiple stacks, then. What I said still applies... you're either not allocating enough space or you're trying to put something in the array at the wrong index. You might want to also check that you actually allocated the space to begin with and that the array isn't null. |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
|
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
Quote:
public Node[] stack; // this is supposedly my array how do i access each stack anyway? i had those T[] (top) and B[] (base pointer) to be able to keep track of the stack but i don't know how to use them to access the stack. Or do i use them to acces the stack? I don't know if you had run my program. but if you could, or somebody else could, i'd appreciate it very much.. |
| ||
| Re: i need help on stack. If pop: stack is empty. newNode.link = top; top = newNode; These lines of code don't make sense. Whatever the current 'top' Node is, you should be assigning it's 'next node' pointer to newNode. Since you're keeping it in an array, you could just drop the whole 'linked list' thing and put your stack in the array. It seems you are mixing concepts. |
| ||
| Re: i need help on stack. If pop: stack is empty. Quote:
I'm pretty sure i am clear of what i want, but can't understand how to do it.. :| thanks for your replies. here's the edited code;
import java.io.*; I can now push strings onto shelf , 2, and 3 BUT i think it is in a single stack.. :( |
| All times are GMT -4. The time now is 11:10 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC