0

I need to call a method that returns an array of strings but I keep getting an error. I did the Arrays.toString but it still is not working.

public class MyStore {

public static void main(String[] args) {
    SalesAssociate salesAssoc = new SalesAssociate("Bob", "Jones", "001");

    System.out.println(Arrays.toString(salesAssoc.getCashPosition()));
}//main
}//class

This is my class and method.

public class SalesAssociate extends FloorAssociate {
// Constructor
public SalesAssociate(String firstName, String lastName, String employeeId) {
        super(firstName, lastName, employeeId);
}
public String[] getCashPosition(){
        String cp[] = new String[3];
        cp[0]= super.getStoreLocation();
        cp[1]= super.getEmployeeId();
        cp[2]= "$3500";
        cp[3]= timeStamp();
        return cp;
    }
}

And this is my error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at indassn3.SalesAssociate.getCashPosition(SalesAssociate.java:38)
at indassn3.MyStore.main(MyStore.java:21)
Java Result: 1

By the way, the super.getStoreLocation, super.getEmployeeId, and timeStamp methods all return strings.

1
  • 4
    Your array has 3 indices. cp[3] is a 4th index. Just change new String[3]; to new String[4]; Commented Apr 13, 2016 at 15:13

5 Answers 5

4
    String cp[] = new String[3];
    cp[0]= super.getStoreLocation();
    cp[1]= super.getEmployeeId();
    cp[2]= "$3500";
    cp[3]= timeStamp();

You are creating an array of length 3 and then try to add 4 elements. An array of length 3 has the indicies 0 to 2

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

1 Comment

thanks, sorry about the stupid mistake, just missed it i guess
1

You declared an array of size new String[3].

This means there is only 3 elements in that string, but you are attempting to set 4 (0, 1, 2, 3). Increse it to new String[4] and it should work just fine.

Comments

1

Use ArrayList without ArrayIndexOutOfBoundsException :)

public static void main(String[] args)
{
    ArrayList<String> cp = new ArrayList();
    cp.add("1"); 
    cp.add("2"); 
    cp.add("3");
    cp.add("4");
    cp.add("5");
}

Comments

1

Create your String array with:

String cp[] = new String[4];

Explanations

The following line:

String cp[] = new String[3];

Creates an array with 3 possible elements:

  • cp[0]
  • cp[1]
  • cp[2]

But a little bit further into the code, you wrote :

cp[3]= timeStamp();

This tries to assign a value to the 4rth element, this is out of bound and thus throws an ArrayIndexOutOfBoundsException. Remember that the first element is at position 0 in your array.

Comments

0

In your given code

   String cp[] = new String[3];
    cp[0]= super.getStoreLocation();
    cp[1]= super.getEmployeeId();
    cp[2]= "$3500";
    cp[3]= timeStamp(); //problem with this code actually.

This Statement new String[3] Creates a array of String of length 3. So, accessible index would be 0, 1 and 2.

but cp[3]= timeStamp(); in this code you try to access index 3 which is illegal to java.

So, cp[3]= timeStamp(); this code will raise an Exception ArrayIndexBoundException.

Better to use ArrayList<String> to avoid these kind of exception generally. because ArrayList is auto grow-able. you will never face these kind of issue in future.

ArrayList<String > cp= new ArrayList<>();
cp.add(super.getStoreLocation());
cp.add(super.getEmployeeId());
cp.add("$3500");
cp.add(timeStamp()); // and so on. No need to define the size at early.

Comments

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.