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.
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
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.
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.
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");
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.