-2

Im having trouble with my stack program and I don't really understand my errors. Can I please get some help? Every time i try to run it, it has multiple errors and im extremely confused on a few things. the errors i have is a total of 60 errors. therefore the whole program is off. All im trying to do is to create names in this stack and queue program.

public class YourStackNB {
private int maxSize;
private long[] stackArray;
private int top;

public YourStackNB(int s) {
  maxSize = s;
  stackArray = new long[maxSize];
  top = -1;
}
public void push(long j) {
  stackArray[++top] = j;}
public long pop() {
 return stackArray[top--];
  }
 public boolean isEmpty() {
  return (top == -1);
}
   public static void main(String[] args) {
  YourStackNB theStack = new YourStackNB();
  Stack<Name> even = new Stack<>();
  theStack.push(dino);
  theStack.push(perry);
  theStack.push(jada);
  theStack.push(holly);
  theStack.push(nori);
  
  while (!theStack.isEmpty()) {
     long value = theStack.pop();
     System.out.print(value);
     System.out.print(" ");
  }
  System.out.println("");
}

 }
Nori
  • 21
  • 3
  • 2
    We're not mind-readers here: if you're "getting multiple errors and ... confused on a few things", you'll need to **tell us** what errors you're getting and what things you're confused about. – Kevin Anderson Sep 18 '20 at 04:09
  • 1
    Please [edit] your question to add the error messages and the cause of the confusion. – Modus Tollens Sep 18 '20 at 04:17

1 Answers1

0

You are trying to implement stack using array. But you are not using it properly. Rather you are trying to use java.util.Stack with passing incorrect type Name.

Another thing you are trying to implement Stack of long data type. But you are trying to put some string there. These codes are incorrect

//  Stack<Name> even = new Stack<>();
//  theStack.push(dino);
//  theStack.push(perry);
//  theStack.push(jada);
//  theStack.push(holly);
//  theStack.push(nori);

I can suggest you please try to write some basic java program so that you are more aware of java syntax and it will help you to better thinking.

Just for your learning I am fixing the errors and this code can be implemented in better way

public class YourStackNB {
private int maxSize;
private long[] stackArray;
private int top;

public YourStackNB(int s) {
  maxSize = s;
  stackArray = new long[maxSize];
  top = -1;
}
public void push(long j) {
  stackArray[++top] = j;}
public long pop() {
 return stackArray[top--];
  }
 public boolean isEmpty() {
  return (top == -1);
}
   public static void main(String[] args) {
  YourStackNB theStack = new YourStackNB(10);

  theStack.push(100);
  theStack.push(200);
  theStack.push(300);
  theStack.push(400);
  theStack.push(500);
//  Stack<Name> even = new Stack<>();
//  theStack.push(dino);
//  theStack.push(perry);
//  theStack.push(jada);
//  theStack.push(holly);
//  theStack.push(nori);
  
  while (!theStack.isEmpty()) {
     long value = theStack.pop();
     System.out.print(value);
     System.out.print(" ");
  }
  System.out.println("");
}

 }
OUTPUT:
    500 400 300 200 100

As stack is last in first out(LIFO) thus it's printing in reverse order we inserted the element into it.

Kousik Mandal
  • 686
  • 1
  • 6
  • 15
  • i see my mistakes but question, where would i input the names? if my codes are incorrect? – Nori Sep 18 '20 at 04:30
  • Names are String data type not long data type, thus if you need to put String data type then you need to implement the stack of string rather than long. There are ways we can implement Stack using generics. But as a beginner will suggest you to implement it type specific first. – Kousik Mandal Sep 18 '20 at 04:36
  • would i use stack.add? i need names on my program and im trying to teach myself. – Nori Sep 18 '20 at 04:51