Kalle
Kalle

Reputation: 29

Can't seem to make it work. Can't get the median out as I want

I can't get the median out. I want the median from the words. Having a hard time getting the value out of the for loop.

public class MedianWord {
    static double medianWordLength(String words) {
        String[] parts = words.split(" ");
        int[] a;
        double median = 0;

        for (int i = 0; i < parts.length; i++) {
            a = new int[parts[i].length()];
            Arrays.sort(a);
            if (a.length % 2 == 0) {
                median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
            }
            else
                median = (double) a[a.length / 2];
        }
        return median;
    }
}

Upvotes: 2

Views: 82

Answers (4)

WJS
WJS

Reputation: 40034

First you keep sorting the array over and over again and then replacing the array within the loop. That is your main problem.

But to facilitate the process, you just need one array and no for loop. Just sort the word array based on length of the words. I left some print statements in so you could see what's going on. And if you split on one or more spaces you don't need to be as careful when you enter the string of words.

String quote = "To be or not to be that is the question";
System.out.println(medianWordLength(quote));
    
static double medianWordLength(String words) {
    String[] parts = words.split("\\s+");
    double median = 0;

    // sort the array based on length of the words
    Arrays.sort(parts,
            (a, b) -> Integer.compare(a.length(), b.length()));

    for (String v : parts) {
        System.out.println(v);
    }
    System.out.println("------------------");

    // get the "middle" index.
    int idx = parts.length / 2;

    // adjust index based on array size
    if (parts.length % 2 == 0) {
        System.out.println(parts[idx-1] + " " + parts[idx]);
        median = (parts[idx-1].length() + parts[idx].length())
                / 2.;
    } else {
        System.out.println(parts[idx]);
        median = parts[idx + 1].length();
    }
    return median;
}

Prints

To
be
or
to
be
is
not
the
that
question
----------
be is
2.0

Upvotes: -1

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

public static double medianWordLength(String str) {
    int[] arr = Arrays.stream(str.split("\\s+"))
                      .mapToInt(String::length)
                      .sorted()
                      .toArray();

    int mid = arr.length / 2;
    double median = (double)arr[mid];

    return arr.length % 2 == 0 ? (median + arr[mid - 1]) / 2 : median;
}

Upvotes: 0

Mauricio Z&#225;rate
Mauricio Z&#225;rate

Reputation: 440

That should work fine:

static Integer getMedian(String sentence) {
        String[] str = sentence.split(" ");
        Integer[] strLen = new Integer[str.length];

        for (int i = 0; i < strLen.length; i++) {
            strLen[i] = str[i].length();
        }

        return strLen.length % 2 == 0?
                (strLen[strLen.length / 2] + strLen[strLen.length / 2 - 1]) / 2 : strLen[strLen.length / 2];
    }

Upvotes: 1

Mureinik
Mureinik

Reputation: 311188

Each iteration of the loop overwrites the a array with a new array. You need to break the problem into two parts - first, iterate over the parts array and convert it to an array a of lengths, and then find the median of that array:

static double medianWordLength(String words) {
    String[] parts = words.split(" ");
    int[] a = new int[parts.length];
    for (int i = 0; i < parts.length; i++) {
        a[i] = parts[i].length();
    }

    Arrays.sort(a);
    if (a.length % 2 == 0) {
        return ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
    }
    else {
        return a[a.length / 2];
    }
}

Side note:
Converting the words to a sorted array of lengths could be done (arguably) more elegantly with streams:

int[] a = Arrays.stream(words.split(" ")).mapToInt(String::length).sorted().toArray();

Upvotes: 2

Related Questions