| | |
i need help on stack. If pop: stack is empty.
![]() |
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
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..
this is another class. This has the main type:
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..
java Syntax (Toggle Plain Text)
public class MStack implements Stack{ public Node top; public Node[] stack; public static int T[]; public static int B[]; public static int oldT[]; public static int i; int size2; int noOfStack; class Node{ Object info; Node link; } public MStack(){ } public MStack(int size, int noOfStack){ size2= size; stack = new Node[size]; T = new int[noOfStack + 1]; B = new int[noOfStack + 1]; for (int i = 0; i < noOfStack+1; i++){ B[i]= (int)Math.floor(size/noOfStack) * i -1; T[i] = B[i]; } } public Object top(int i){ if (isEmpty(i)){ System.out.println("Stack empty."); }return top.info; } public void push(String item,int i ) { if (T[i]== B[i+1]){ System.out.println("Stack"+ i + "is full."); }else{ Node newNode = new Node(); newNode.info = item; newNode.link = top; top = newNode; System.out.println("You have pushed an element into stack"); } } public Object pop(int i ){ Node temp[] = new Node[noOfStack]; if (isEmpty(i)){ System.out.println("Stack has nothing inside it"); } temp[i] = top; top = top.link; return temp[i].info; } public boolean isEmpty(int i){ return (T[i] == B[i]); } public int size(int i){ int size = T[i] - B[i] ; return size; } }
this is another class. This has the main type:
java Syntax (Toggle Plain Text)
import java.io.*; public class BookShelf extends MStack { public static InputStreamReader ir = new InputStreamReader(System.in); public static BufferedReader bf = new BufferedReader(ir); public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static MStack bookShelf; public static int size; public static int shelf; public static void main (String []args) throws IOException{ System.out.println("What size do you want your bookshelf to be?(integer):"); size = Integer.parseInt(in.readLine()); System.out.println("How many shelves do you want your bookshelf to have?:"); shelf = Integer.parseInt(in.readLine()); bookShelf = new MStack(size, shelf); new BookShelf(); } public BookShelf(){ super(); options(); } public void options(){ try{ do{ System.out.println("\nPlease choose from below. Use numbers to choose."); System.out.println("1. Place book on shelf"); System.out.println("2. Retrieve book from shelf"); System.out.println("3. View books on each shelf"); System.out.println("4. Exit"); int choice = Integer.parseInt(in.readLine()); if (choice == 1){ System.out.println("Which shelf do you want to place a book?"); System.out.println("Enter either 1, 2 or 3"); int placement = Integer.parseInt(in.readLine()); if(placement == 1){ System.out.println("Enter element:"); String item = bf.readLine(); garwick(placement); bookShelf.push(item, placement); }if(placement == 2){ System.out.println("Enter an element:"); String item = bf.readLine(); garwick(placement); bookShelf.push(item, placement); }if(placement == 3){ System.out.println("Enter an element:"); String item = bf.readLine(); garwick(placement); bookShelf.push(item, placement); } }else if (choice == 2){ System.out.println("Which shelf do you want to retrieve a book, 1, 2, or 3?"); int choice2 = Integer.parseInt(in.readLine()); switch (choice2){ case 1 : if (bookShelf.isEmpty(choice2)) { System.out.println( " Shelf " + choice2 + " is empty!"); }else { System.out.println("Item" + bookShelf.pop(choice2)+ " is retrieved."); }break; case 2 : if (bookShelf.isEmpty(choice2)) { System.out.println(" Shelf " + choice2 + " is empty!"); }else { System.out.println("Item" + bookShelf.pop(choice2)+ " is retrieved."); }break; case 3 : if (bookShelf.isEmpty(choice2)) { System.out.println(" Shelf " + choice2 + " is empty!"); }else { System.out.println("Item" + bookShelf.pop(choice2)+ " is retrieved."); }break; } }else if(choice == 3){ System.out.println("Recent items"); for (int i = 1; i <= shelf; ++i) { System.out.println("shelf #" + i + ": " + bookShelf.top(i)); } System.out.println("shelf #" + i + ": Empty"); }else if (choice == 4){ System.out.println("You have exited. Thank you!"); break; } }while (true); }catch(IOException e){} } /* * Method that implements garwick's algorithm for reallocating free space * within a multiple stack */ public void garwick(int i) { int diff[] = new int[shelf + 1]; int size[] = new int[shelf + 1]; int totalSize = 0; double freecells, incr = 0; double alpha, beta, sigma = 0, tau = 0; /* Compute for the allocation factors */ for (int j = 1; j <= bookShelf.noOfStack; j++) { size[j] = bookShelf.size(j); if ((bookShelf.T[j] - bookShelf.oldT[j]) > 0){ diff[j] = bookShelf.T[j] - bookShelf.oldT[j]; }else{ diff[j] = 0; totalSize += size[j]; incr += diff[j]; } } diff[i]++; size[i]++; totalSize++; incr++; freecells = bookShelf.size2 - totalSize; alpha = 0.10 * freecells / bookShelf.noOfStack; beta = 0.90 * freecells / incr; /* If all stacks are full */ if (freecells < 1) throw new StackException("Stack overflow: All Stacks are full"); /* Compute for new bases */ for (int j = 2; j <= bookShelf.noOfStack; j++) { tau = sigma + alpha + diff[j - 1] * beta; bookShelf.B[j] = bookShelf.B[j - 1] + size[j - 1] + (int) Math.floor(tau)- (int) Math.floor(sigma); sigma = tau; } /* Restore size of the overflowed stack to its old value */ size[i]--; System.out.println("size[" + i + "] = " + size[i]); /* Compute for new top addresses */ for (int j = 1; j <= bookShelf.noOfStack; j++) { bookShelf.T[j] = bookShelf.B[j] + size[j]; } bookShelf.oldT = bookShelf.T; } }
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..
Last edited by ezkonekgal; Jan 12th, 2009 at 9:11 am.
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
•
•
•
•
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.
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)
Last edited by ezkonekgal; Jan 13th, 2009 at 12:02 am.
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
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.
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.
Last edited by ezkonekgal; Jan 13th, 2009 at 12:22 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
![]() |
Similar Threads
- Stack with Dynamic Array (C++)
- C++ Stack/GeneralTree help (C++)
- Dynamic stack in c++ (C++)
- please i need your help. (C++)
- please help me to rewrite this. (C++)
- at the moment this is a stack of queue, i am trying to make it into first in first ou (C++)
- Mathematics In A Stack (C++)
- Stack Queue Fstream (C++)
- Reverse Output (Stack) (C++)
Other Threads in the Java Forum
- Previous Thread: need help.....
- Next Thread: Simple Scanner Class Question
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows







) im sorry i had to speak in our dialect... its just that i cud express myself better that way, sorry