Reputation: 11
I'm working on an exercise that requires a method to sum up the all durations for all phone numbers in the data (ie similar to caller ID). I've managed to sum up duration, however not all the data are being printed. I've been trying to figure why this is but I can't wrap my head around it. Any help would be greatly appreciated. Below is the code:
//initial code provided
public static void main(String[] args) {
String[] phoneNumbers = new String[100];
int[] callDurations = new int[phoneNumbers.length];
int size = 0;
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-8888", 10);
size = addCall(phoneNumbers, callDurations, size, "555-555-8888", 10);
size = addCall(phoneNumbers, callDurations, size, "555-555-7777", 10);
}
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;
}
//the portion of code that I'm trying to write
public static void totalDurations(String[] phoneNumbers, int[]
callDurations, int size) {
String[] copyNum = new String[phoneNumbers.length];
int[] copyDur = new int[phoneNumbers.length];
int newSize = size;
int pos = 1; //counter for next available empty cell
copyNum[0] = phoneNumbers[0];
copyDur[0] = callDurations[0];
for (int i = 0; i < newSize; i++){
for (int j = 1; j < size; j++){
if (copyNum[i] != phoneNumbers[j]){
copyNum[i+pos] = phoneNumbers[j];
pos++;
}
else {
copyDur[i] += callDurations[j];
newSize = newSize -1;
}
}
System.out.println(copyNum[i] + ":" + copyDur[i]+ "s");
}
}
my current output
Total Durations:
555-555-5555:137s
555-555-0000:12s
555-555-1234:26s
555-555-8888:20s
Upvotes: 1
Views: 70
Reputation: 335
First the code is very hard to read/understand and error prone. I would use the class and built in data type like Map and List.
public static class CallDuration {
String phoneNumber;
int duration;
public CallDuration(String phoneNumber, int duration) {
this.phoneNumber = phoneNumber;
this.duration = duration;
}
}
public static void main(String[] args) {
List<CallDuration> callDurations = new ArrayList<>();
callDurations.add(new CallDuration("555-555-5555", 137));
callDurations.add(new CallDuration("555-555-0000", 12));
callDurations.add(new CallDuration("555-555-1234", 26));
callDurations.add(new CallDuration("555-555-8888", 10));
callDurations.add(new CallDuration("555-555-8888", 10));
callDurations.add(new CallDuration("555-555-7777", 10));
printCallDuration(callDurations);
}
private static void printCallDuration(List<CallDuration> callDurations) {
Map<String, Integer> totalCallDurationMap = new HashMap<>();
for (CallDuration callDuration : callDurations) {
Integer value = totalCallDurationMap.computeIfAbsent(callDuration.phoneNumber, x -> 0);
totalCallDurationMap.put(callDuration.phoneNumber, value + callDuration.duration);
}
for (Map.Entry<String, Integer> entry : totalCallDurationMap.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}
Upvotes: 1
Reputation: 2907
This is some really ugly code and you definitely need to run it in a debugger, but it's clear to me that the error is with the line
newSize = newSize - 1;
This decrements the variable that is being used in the outer for
loop. If you only got through four out of six elements, that means the else
clause was entered for two of them. Perhaps you meant to decrement size
instead?
Think about what you want to do here. Use a debugger, and see where things go wrong in a way you aren't expecting. Correct them.
Upvotes: 1