943,949 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2173
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 13th, 2009
0

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).
Last edited by BestJewSinceJC; Jan 13th, 2009 at 4:22 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jan 14th, 2009
0

Re: i need help on stack. If pop: stack is empty.

Y

Why do you need to give the 'push' method an index, anyway? .
Because i was thinking that if the index is 1, then it would refer to the stack 1 and it would push the item in that stack.
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Jan 14th, 2009
0

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jan 15th, 2009
0

Re: i need help on stack. If pop: stack is empty.

Click to Expand / Collapse  Quote originally posted by SamCris ...
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
Actually , i also didn't undertsand much about the T[] and oldT[] discussed int our lectures. As i know, T[] refers to the new tops that are computed using the garwick also in allocating memery. oldT[] will piont to the tops before the memory allocation happened.
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Jan 15th, 2009
0

Re: i need help on stack. If pop: stack is empty.

So you have multiple stacks, then.
That's what im trying to do in the first place.

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.
i did this:
java Syntax (Toggle Plain Text)
  1. public Node[] stack; // this is supposedly my array
  2. stack = new Node[size2]; // this is what i had to initialize the size of the stack, in which the user prompts.

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..
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Jan 15th, 2009
0

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.
Last edited by BestJewSinceJC; Jan 15th, 2009 at 12:36 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jan 16th, 2009
0

Re: i need help on stack. If pop: stack is empty.

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.
I can't do that, cause my job has to be linked list.
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;

java Syntax (Toggle Plain Text)
  1.  
  2. public class MStack implements Stack{
  3.  
  4. public Node top;
  5. public Node[] stack;
  6. public int T[];
  7. public int B[];
  8. public int oldT[];
  9. public static int i;
  10. int size2;
  11. int noOfStack2;
  12.  
  13.  
  14. class Node{
  15. Object info;
  16. Node link;
  17. }
  18.  
  19. public MStack(){
  20.  
  21. }
  22.  
  23. public MStack(int size, int noOfStack){
  24. size2= size;
  25. noOfStack2 = noOfStack;
  26. stack = new Node[size2];
  27. T = new int[noOfStack2 + 1];
  28. B = new int[noOfStack2 + 1];
  29. oldT = new int[noOfStack2 + 1];
  30. for (int i = 0; i < noOfStack2; i++){
  31. B[i]= (int)Math.floor(size2/noOfStack2) * i -1;
  32. T[i] = B[i];
  33. B[noOfStack2] = size2- 1;
  34. }B[noOfStack2] = size2 - 1;
  35. }
  36.  
  37. public Object top(int i){
  38. return top.info;
  39. }
  40.  
  41. public void push(String item,int i ) {
  42. if (T[i]== B[i+1]){
  43. System.out.println("Stack"+ i + "is full.");
  44. }else{
  45. stack[i] = new Node();
  46. stack[i].info = item;
  47. stack[i].link = top;
  48. top =stack[i];
  49. System.out.println("You have pushed " + item + " into stack!");
  50. T[i]++;
  51. }
  52. }
  53.  
  54. public Object pop(int i ){
  55. Node temp;
  56. if (isEmpty(i))
  57. throw new StackException("Stack underflow.");
  58. temp = top;
  59. top = top.link;
  60. return temp.info;
  61. }
  62.  
  63. public boolean isEmpty(int i){
  64. boolean ques = false;
  65. if(T[i] == B[i]){
  66. ques = true;
  67. }
  68. return ques;
  69.  
  70. }
  71.  
  72. public int size(int i){
  73. int size[]= new int[i];
  74. size[i] = T[i] - B[i] ;
  75. return size[i];
  76. }
  77.  
  78. }

java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. public class BookShelf extends MStack {
  4.  
  5. public InputStreamReader ir = new InputStreamReader(System.in);
  6. public BufferedReader bf = new BufferedReader(ir);
  7. public BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  8. public MStack bookShelf;
  9. public int size;
  10. public int shelf;
  11.  
  12. public static void main (String []args) throws IOException{
  13. new BookShelf();
  14. }
  15.  
  16. public BookShelf()throws IOException{
  17. super();
  18. options();
  19. }
  20.  
  21. public void options()throws IOException{
  22. System.out.println("What size do you want your bookshelf to be?Integers only.:");
  23. size = Integer.parseInt(in.readLine());
  24. System.out.println("How many shelves do you want your bookshelf to have?:");
  25. shelf = Integer.parseInt(in.readLine());
  26. bookShelf = new MStack(size, shelf);
  27. System.out.println("You have now a book shelf with " +size+ " capacity and " +shelf+ " shelves" );
  28. try{
  29. do{
  30. System.out.println("\nPlease choose from below. Use numbers to choose.");
  31. System.out.println("1. Place book on shelf");
  32. System.out.println("2. Retrieve book from shelf");
  33. System.out.println("3. Show items on shelf");
  34. System.out.println("4. Exit");
  35. int choice = Integer.parseInt(in.readLine());
  36. if (choice == 1){
  37. try{
  38. System.out.println("Which shelf do you want to place a book?(Integers only");
  39. int placement = Integer.parseInt(in.readLine());
  40. int i;
  41. if(placement == 1){
  42. i = 0;
  43. System.out.println("Enter element:");
  44. String item = bf.readLine();
  45. bookShelf.push(item,i);
  46. }if(placement == 2){
  47. i = 1;
  48. System.out.println("Enter an element:");
  49. String item = bf.readLine();
  50. bookShelf.push(item,i);
  51. }if(placement == 3){
  52. i = 2;
  53. System.out.println("Enter an element:");
  54. String item = bf.readLine();
  55.  
  56. bookShelf.push(item,i);
  57. }
  58. }catch(ArrayIndexOutOfBoundsException e){
  59.  
  60. }
  61. }else if (choice == 2){
  62. System.out.println("Which shelf do you want to retrieve a book? Integers only.");
  63. int choice2 = Integer.parseInt(in.readLine());
  64. try{
  65. switch (choice2){
  66. case 1 : if (bookShelf.isEmpty(choice2)) {
  67. System.out.println("Shelf is empty");
  68. break;
  69. }else{
  70. System.out.println("Item " + bookShelf.pop(choice2)+ " is retrieved.");
  71. break;
  72. }
  73. case 2 : if (bookShelf.isEmpty(choice2)) {
  74. System.out.println("Shelf is empty");
  75. break;
  76. }else{
  77. System.out.println("Item " + bookShelf.pop(choice2)+ " is retrieved.");
  78. break;
  79. }
  80. case 3 : if (bookShelf.isEmpty(choice2)) {
  81. System.out.println("Shelf is empty");
  82. break;
  83. }else{
  84. System.out.println("Item " + bookShelf.pop(choice2)+ " is retrieved.");
  85. break;
  86. }
  87. }
  88. }catch(NullPointerException e){
  89.  
  90. }
  91. }else if (choice == 3){
  92. for (int i = 0; i <= shelf; ++i) {
  93. if (!isEmpty(i)){
  94. System.out.println("shelf #" + i + ": " + bookShelf.pop(i));
  95. }else{
  96. System.out.println("shelf #" + i + ": is empty.");
  97. break;
  98. }
  99. }
  100.  
  101. }else if ( choice == 4){
  102. System.out.println("You have exited. Thank you!");
  103. break;
  104. }
  105. }while (true);
  106. }catch(IOException e){}
  107. }
  108.  
  109.  
  110. }

I can now push strings onto shelf , 2, and 3 BUT i think it is in a single stack..
Last edited by ezkonekgal; Jan 16th, 2009 at 8:04 am.
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: need help.....
Next Thread in Java Forum Timeline: Simple Scanner Class Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC