1

Hi guys I'm having a problem getting the right output for an assignment. I am looking for a way to copy a set of elements from one parallel array (which contains a string--int) to the other with no duplicate value. For example: I have these set of parallel arrays

This is the ORIGINAL PARALLEL ARRAY :

String[] phoneNumbers;           
    phoneNumbers = new String[100];
    int[] callDurations = new int[phoneNumbers.length];
    int size = 0;

    phoneNumbers[0] = "888-555-0000";
    callDurations[0] = 10;
    phoneNumbers[1] = "888-555-1234";
    callDurations[1] = 26;
    phoneNumbers[2] = "888-555-1234";
    callDurations[2] = 2;
    size = 3; 

I want to create a method that creates new pairs of parallel arrays from the original arrays (phoneNumber & callDuration).This method would be called totalDuration and it would return no value(void). It would check if a number from the current array is in the new array, if yes, it will just add any duplicate duration to the current duration. if no, it would add a new element to the NewNumber array and add an element to the NewDuration array.

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;
}

This find method would be used to check if a phone number has already been placed in the new arrays, and if so, to determine where that number is.

For example, if the array contains

    phoneNumbers[0] = "888-555-0000";
    callDurations[0] = 10;
    phoneNumbers[1] = "888-555-1234";
    callDurations[1] = 26;
    phoneNumbers[2] = "888-555-1234";
    callDurations[2] = 2;

printing calls details for "888-555-1234" would look like:

    all calls from: 
Calls from 888-555-1234:
888-555-1234 duration: 26s
888-555-1234 duration: 2s

the output for the new method instead should be(26s +2s):

    all calls from: 
Calls from 888-555-1234:
888-555-1234 duration: 28s

I tried to solve it with this code but its giving a wrong output:

public static void totalDurations(String[] phoneNumbers, int[] callDuration, int size, String target) {
    String[] NewNumbers;
    int[] NewDuration;
    int pos;

    NewNumbers = new String[phoneNumbers.length];
    NewDuration = new int[callDuration.length];
    pos = find(phoneNumbers,size, 0,target);
    while(pos < size && !target.equals(phoneNumbers[pos])) {
        NewNumbers[pos] = phoneNumbers[pos];
        NewDuration[pos] = callDuration[pos];
        System.out.println(NewNumbers[pos] + "duration" + NewDuration[pos] +"s");
    }

}

NOT-RELATED The code I use to get all my details for each call is my method "findAllCalls"

public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) {
    int matchPos;

    System.out.println("Calls from " + targetNumber + ":");
    matchPos = find(phoneNumbers, size, 0, targetNumber);
    while (matchPos >= 0) {
        System.out.println(phoneNumbers[matchPos] + " duration: " + callDurations[matchPos] + "s");


        matchPos = find(phoneNumbers, size, matchPos + 1, targetNumber);
    }
}

System.out.println("\n all calls from: ");
    findAllCalls(phoneNumbers,callDurations,size,"888-555-1234");

Any correction would be much appreciated in advance.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Oluwatosin
  • 170
  • 2
  • 11
  • I havn't gone through your code but can you solve it using set ? – SacJn Sep 21 '15 at 04:53
  • no, it has to be through a method – Oluwatosin Sep 21 '15 at 04:56
  • Rather than using an array, i would suggest using an array of class which has number, list of callDurations and total duration at last. not much effort indeed. But you need to maintain a map for number string to its corresponding object – SacJn Sep 21 '15 at 05:06
  • it can be done through Hashmap also...i think it would be easier with that. – Sachin Sep 21 '15 at 05:09
  • yes using maps would definitely make it easier. I wish I could use it, but I have to follow the instructions on the assignment. This is like the 5th and last method I'm creating – Oluwatosin Sep 21 '15 at 05:16
  • Java is an Object Oriented language. Emphasis on **Object**. Don't create parallel arrays, define a class, e.g. `Call`, with two fields, e.g. `phoneNumber` and `duration`, or a class like @SacJn suggested. --- If it's from an assignment, then whoever created that assignment needs to learn [Object Oriented Programming](https://en.wikipedia.org/wiki/Object-oriented_programming). --- Perhaps you could teach the teacher a lesson. ;-) – Andreas Sep 21 '15 at 05:25

1 Answers1

0

Well according to your question the method for printing total duration of a target number can be like following

    public static int numberIndex(String[] numbers, String target) {
        for(int i = 0; i < numbers.length; i++) {
            if(numbers[i].equals(target)) {
                return i;
            }
        }

        return -1;
    }

    public static void totalDuration(String[] phoneNumbers, int[] callDurations, String target) {
        String[] newNumbers = new String[phoneNumbers.length];
        int[] newDurations = new int[callDurations.length];
        int newIndex = 0;

        for(int i = 0; i < phoneNumbers.length; i++) {
            int oldIndex = numberIndex(newNumbers, phoneNumbers[i]);

            if(oldIndex == -1) {
                newNumbers[newIndex] = phoneNumbers[i];
                newDurations[newIndex] = callDurations[i];
                newIndex++;
            }
            else {
                newDurations[oldIndex] += callDurations[i];
            }
        }

        for(int i = 0; i < newIndex; i++) {
            System.out.println("Total duration for " + newNumbers[i] + ": " + newDurations[i]);
        }
    }

As you mentioned that the return type of this method should be void, so I assumed you only need to print the total duration. For that there is no need to construct new arrays like NewNumbers or NewDurations.

If holding them in arrays is absolutely necessary, let me know in the comment.

Ashraf Purno
  • 1,065
  • 6
  • 11