Saad Saad - 2 months ago 13
Java Question

Written program but can't find the bug. Array capacity exceeded

So i am writing a small program where i have to show the total durations of each phone call.The phone numbers have their array where as the durations also have their own array. It is parallel arrays. For instance, if i receive a phone call from 555-1234 and the duration is 10 minutes and then receive call from another person (555-4567) and the duration is 3 minutes and finally another call from 555-1234 and this time the duration is 5 minutes. My program should print the total durations like this
555-1234 total duration 15 minutes
555-4567 total duration 3 minutes.

I have written the code here.

public class Activity0D {

public static void main(String[] args) {
String[] phoneNumbers = new String[100];
int[] callDurations = new int[phoneNumbers.length];
int size = 0;

for(int i = 0; i<=99; i++){
size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 137);
//size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 12);
//size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
//size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 382);
}
totalDurations(phoneNumbers, callDurations, size);
}

public static int addCall(String[] phoneNumbers, int[] callDurations, int size, String newNumber, int newDuration) {
if (size >= phoneNumbers.length) {
System.out.println("Error adding " + newNumber + ": array capacity exceeded.");
} else {
phoneNumbers[size] = newNumber;
callDurations[size] = newDuration;
size++;
}

return size;
}

public static void printList(String[] phoneNumbers, int[] callDurations, int size) {
for (int i = 0; i < size; i++) {
System.out.println(phoneNumbers[i] + " duration: " + callDurations[i] + "s");
}
}

public static int find(String[] list, int size, int start, String target) {
int pos = start;

while (pos < size && !target.equals(list[pos])) {
pos++;
}

if (pos == size){
pos = -1;
}
return pos;
}
public static void totalDurations (String[] phoneNumbers,int[] callDurations,int size){

String [] phoneNum = new String [phoneNumbers.length];
int [] callDur = new int [callDurations.length];
int newPos = 0;

for (int i = 0; i < phoneNumbers.length; i++) {
int pos = find (phoneNum, size, 0, phoneNumbers[i]);

if (pos == -1){
newPos = addCall(phoneNum, callDur, size, phoneNumbers[i], callDurations[i]);
}else {
callDur[pos] += callDurations[i];
}
}
printList(phoneNum, callDur, newPos);
}

}


I am getting array capacity exceeded error. I ran through the program a couple of times but still couldn't remove the bug. I was wondering if anyone could point out the error for me. Or may be just help me. I can further clarify the question if need be. Thanks

Answer

i have looked at code and the problem is , when you run the addCall in the first for loop from 0 to 99 , then the size gets to 100 by the time it reaches the end of the loop, and then when totalDuration method is called, the size is still 100 which is more than the array length and so you get the error.

i have not understood the logic yet, but depending on what you want may be you want to reset the size to zero.

EDIT :

so its just the extension of what i told above, you just need to reset the size

 public static void main(String[] args) {
  String[] phoneNumbers = new String[100];
  int[] callDurations = new int[phoneNumbers.length];
  int size = 0;

 for(int i = 0; i<=99; i++){
   size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 137);
 }
 size = 0;
 totalDurations(phoneNumbers, callDurations, size); 
  }  

After this your total duration method will just run fine, i ran your method, there would be some problem in logic which you need to solve, also you are creating extra arrays for this which is not a good sign of programming. But all the best, programming is fun! :)