I'm coding a stack implementation using Arraylist. I'm trying to use NoSuchElementException when it tries to pop when the array is empty but I get a build error message and I have no idea what is going on. Here is the output I'm getting:
please enter your number:
1 f
*********************Stack ArrayList Implementation*********************
false
1
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:422)
at java.util.ArrayList.get(ArrayList.java:435)
at StackUsingArrayList.peek(StackUsingArrayList.java:42)
at StackUsingArrayList.main(StackUsingArrayList.java:74)
C:\Users\alsrb\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)
this part seems to be the problem and the funny thing is if I don't use exception throws, it works just fine.
int pop() {
if (!isEmpty()) { // checks for an empty Stack
int popValue = stackList.get(stackList.size() - 1);
stackList.remove(stackList.size() - 1); // removes the poped element
return popValue;
} else {
throw new NoSuchElementException();
//System.out.print("The stack is already empty ");
//return -1;
}
}
Somebody please help me. here's my whole code
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class StackUsingArrayList{
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> stackList;
StackUsingArrayList() {
stackList = new ArrayList<>();
}
void push(int v) {
stackList.add(v);
}
int pop() {
if (!isEmpty()) { // checks for an empty Stack
int popValue = stackList.get(stackList.size() - 1);
stackList.remove(stackList.size() - 1); // removes the poped element
return popValue;
} else {
throw new NoSuchElementException();
//System.out.print("The stack is already empty ");
//return -1;
}
}
boolean isEmpty() {
if (stackList.get(0) == null){
return true;
} else {
return false;
}
}
int peek() {
return stackList.get(stackList.size() - 1);
}
int size(){
int i = 0;
while(stackList != null){
stackList.get(i);
i++;
}
return i;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StackUsingArrayList myStack = new StackUsingArrayList();
System.out.println("Please enter your number: ");
while(scanner.hasNextInt()){
int x = scanner.nextInt();
if(x >= 0){
myStack.push(x);
}
}
System.out.println("*********************Stack ArrayList Implementation*********************");
System.out.println(myStack.isEmpty());
System.out.println(myStack.peek());
System.out.println(myStack.pop());
System.out.println(myStack.peek());
System.out.println(myStack.pop());
System.out.println(myStack.peek());
System.out.println(myStack.pop());
}
}
Are you sure it works without the exeception? From what I see you have used the input "1", which fills you ArrayList with one element. In your main method you peek (returns "1") and then pop (returns "1"). And then you continue to peek and pop, resulting in the next peek call to execute
int peek() {
return stackList.get(stackList.size() - 1);
}
Since you already popped the only element in the list, the list size is 0. That means you try to return stackList.get(0-1), which correctly throws the ArrayIndexOutOfBoundsException.
You could add a check in the peek function to see if there are elements left in the list. If not, it might be a good idea to return null.
Besides that, it might be a good idea to have a look at the ArrayList methods provided by java. Specifically ArrayList.size() and ArrayList.isEmpty() are good alternatives to use.