package com.abc.text;

public class StringProcessing {
    /**
     * Returns the number of non-null strings in the set.
     * If null or a zero-length array is passed in, then zero is returned.
     * If a slot in the array is null, then it is ignored (not counted).
     */
    public static int getCount(String[] set) {
    }

    /**
     * Returns the total length of all the non-null strings in the set.
     * If null or a zero-length array is passed in, then zero is returned.
     * If a slot in the array is null, then it is ignored (not counted).
     */
    public static int getTotalLength(String[] set) {
    }

    /**
     * Returns the average length of all the non-null strings in the set.
     * If null or a zero-length array is passed in, then zero is returned.
     * If a slot in the array is null, then it is ignored (not counted).
     */
    public static double getAverageLength(String[] set) {
    }

    /**
     * Returns the length of the shortest, non-null string in the set.
     * If null or a zero-length array is passed in, then zero is returned.
     * If a slot in the array is null, then it is ignored (not counted).
     */
    public static int getShortestLength(String[] set) {
    }

    /**
     * Returns the shortest, non-null string in the set (which was found first).
     * If two strings in the set are of the same length, then the first one
     * found is the one returned.
     * If null or a zero-length array is passed in, then null is returned.
     * If a slot in the array is null, then it is ignored (not counted).
     * If every slot in the array is null, then null is returned.
     */
    public static String getFirstShortest(String[] set) {
    }
    
    /**
     * Returns the length of the longest, non-null string in the set.
     * If null or a zero-length array is passed in, then zero is returned.
     * If a slot in the array is null, then it is ignored (not counted).
     */
    public static int getLongestLength(String[] set) {
    }

    /**
     * Returns the longest, non-null string in the set (which was found first).
     * If two strings in the set are of the same length, then the first one
     * found is the one returned.
     * If null or a zero-length array is passed in, then null is returned.
     * If a slot in the array is null, then it is ignored (not counted).
     * If every slot in the array is null, then null is returned.
     */
    public static String getFirstLongest(String[] set) {
    
    
    /**
     * Sorts the specified array by string length in ascending order.
     * Shorter strings come before longer strings.
     * Use your own code for bubble sort.
     * If null or a zero-length array is passed in, then nothing is done.
     * If a slot in the array is null, then it is considered to be longer
     * than any actual string (null's sort to be at the end).
     */
    for ( int maxIdx = set.length - 1; maxIdx > 0; maxIdx-- ) {
        boolean swappedSomething = false;
        for ( int i = 0; i < maxIdx; i++ ) {
            if ( set[i] > set[i + 1] ) {
                String tmp = set[i];
                set[i] = set[i + 1];
                set[i + 1] = tmp;
                swappedSomething = true;
            }
        }
        
        if ( swappedSomething == false ) {
            break;
        }
    }
}
    
    public static void sortLength(String[] list) {
    }
    

    // this method is complete, do not alter it
    private static String format(String[] set) {
        if ( set == null ) {
            return "String[] is null";
        }
        StringBuilder sb = new StringBuilder();
        for ( int i = 0; i < set.length; i++ ) {
            if ( i > 0 ) {
                sb.append(", ");
            }
            sb.append(quoteWrap(set[i]));
        }
        return sb.toString();
    }
    
    // this method is complete, do not alter it
    private static String quoteWrap(String s) {
        if ( s == null ) {
            return "null";
        } else {
            return '"' + s + '"';
        }
    }

    // this method is complete, do not alter it
    private static void process(String[] set) {
        System.out.println("Original String[]: " + format(set));
        System.out.printf("  %3d   - count%n", getCount(set));
        System.out.printf("  %3d   - shortest length (%s)%n", 
            getShortestLength(set), quoteWrap(getFirstShortest(set)));
        System.out.printf("  %3d   - longest length (%s)%n", 
            getLongestLength(set), quoteWrap(getFirstLongest(set)));
        System.out.printf("  %5.1f - average length%n", getAverageLength(set)); 
        System.out.printf("  %3d   - total length%n", getTotalLength(set));
        sortLength(set);
        System.out.println("after length sort: " + format(set));
        System.out.println("----------");
    }
    
    // this method is complete, do not alter it
    public static void main(String[] args) {
        process(new String[] { "apple" });
        process(new String[] { "watermelon", "grape" });
        process(new String[] { "pear", "date" });
        process(new String[] { "cherry", "apple", "pear", "grape", "banana" });
        
        // Zero-length strings are OK, they just have a length of zero
        process(new String[] { "" });
        process(new String[] { "", "apple", "", "cherry", "banana" });
        
        // A zero-length array
        process(new String[0]);
        
        // No array at all, just null passed in
        process(null);
        
        // Some slots contain null instead of a String
        process(new String[] { null });
        process(new String[] { null, null });
        process(new String[] { null, "apple", null });
        process(new String[] { null, "", null });
        process(new String[] { null, "apple", null, "cherry", "banana" });
    }
}

I'm currently working on the bubble sort algorithm to sort the length of my string arrays listed at the bottom of the program. Eclipse is telling me that the > operator is undefined for the argument type java.lang.String. How do I go about fixing this error, as I haven't worked much with string arrays, mostly just integer arrays.

well, you don't.
a String is not a numerical value, so you can't use numerical operators on them.
you can however use the 'compareTo' method of the String class.

well, you don't.
a String is not a numerical value, so you can't use numerical operators on them.
you can however use the 'compareTo' method of the String class.

Thanks Stultuske, I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.