0

I finally managed to write my first Java assignment. The point of the assignment was to implement an ArrayList with Strings which was supposed to behave like a stack.

However, when I run the code in Eclipse I get some kind of error saying:

Exception in thread "main" øv2.Stack@c723704
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at øv2.Stack.peek(Stack.java:30)
at øv2.Stack.main(Stack.java:47)

I am not sure why this is happening, here is my code:

public class Stack {

    private ArrayList<String> stringList = new ArrayList<String>();

    RandomStringGenerator rsg = new RandomStringGenerator();

    private void push(String i){
        stringList.add(i);
        }

    private String Pop(){
        if (stringList.size() >= 1){
            String positionInArray = stringList.get(stringList.size() - 1);
            stringList.remove(stringList.size() - 1);
            return positionInArray;
        }

        else {
            return null;
        }

    }

    private String peek(int i){
        if (i >= 0 && i >= (stringList.size() - 1)){
            return stringList.get(stringList.size() - 1 - i);
        }
        else{
            return null;
        }
    }

    private int getSize(){
        return stringList.size();
    }

    public static void main (String [] args){
        Stack stack = new Stack();
        stack.push("1");
        stack.push("2");
        stack.push("3");
        System.out.println(stack);
        System.out.println(stack.peek(3));
        System.out.println(stack.Pop());
        System.out.println(stack);
    }

    }


import java.util.Random;

public class RandomStringGenerator {


    private int randomNumber;

    private Random randomNumberGenerator = new Random();

    public String randomStringGenerator(){
        int randomNumber = randomNumberGenerator.nextInt();
        String randomString = Integer.toString(randomNumber);
        return randomString;
    }

}

Thank you for your time!

3
  • Java is pretty good about their error messages. You are trying to access index location -1 at line 30 in your method peek. Think about what you are doing and why it is causing that. Commented Mar 26, 2014 at 13:23
  • 3
    i >= 0 && i >= (stringList.size() - 1) this makes no sense, your range check is flawed. Commented Mar 26, 2014 at 13:23
  • You should also check if stringList.size() > 0 Commented Mar 26, 2014 at 13:24

5 Answers 5

1

You are calling peek(3), but the code in peek() accesses stringList.size() - 1 - i which is -1 and hence the error.

and I believe your condition for the peek method is also wrong if (i >= 0 && i >= (stringList.size() - 1)). What are you trying to do here?

Sign up to request clarification or add additional context in comments.

Comments

1

I think this is where the error lies:

return stringList.get(stringList.size() - 1 - i);

size = 3. 3-1-3 will go negative. alter this part to get desired output! :)

3 Comments

Thank you so much! I changed that and now it works, but can I ask you something? I thought that when I printed the array to the screen that it would print the strings "1", "2" and "3" since those are the strings in the attay, however it prints to the screen "øv2.stack@3dccbdf7", do you know why that is?
@user3257736: Did you try toString?
Printing the arrayList directly would print the object format of it. You can print the array values by calling toString() on the stack object just as staticx suggested. You can also define how you to want the stack to be printed by defining a toString() method in your class! Hope that explains.
1

The peek method should have the following return: return stringList.get(stringList.size() - i); It failed when you were making peek(3) since you know the ArrayList has only 3 values, and by subtracting 1 besides i, you were trying to access the element on position -1

Comments

0

Your issue is in peek(). I think that you want it to check if i <= (stringList.size() - 1).

if (i >= 0 && i <= (stringList.size() - 1)){ return stringList.get(stringList.size() - 1 - i); }

Comments

-1

Your if() condition is really strange I think you wanted to put if (i >= 0 && i <= (stringList.size() - 1))

1 Comment

This would avoid out of bound errors: stringList.get(stringList.size() - 1 - i); <-- 3-1-3 = -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.