Question

THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...

THIS IS JAVA PROGRAMMING

Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header.

public static int minLength(String[] array)

Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)

Homework Answers

Answer #1
import java.util.Scanner;

public class FindMinLength {

    public static int minLength(String[] array) {
        int minLength = array[0].length();
        for (int i = 0; i < array.length; i++) {
            if (array[i].length() < minLength)
                minLength = array[i].length();
        }
        return minLength;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] strings = new String[10];
        for (int i = 0; i < strings.length; i++) {
            System.out.print("Enter string " + (i + 1) + ": ");
            strings[i] = in.nextLine();
        }
        System.out.println("Length of smallest length string is " + minLength(strings));
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper...
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper and lower case) and digit ('1' to '3') characters in a String. The method header is as follows: public static int countNumAlphabetic ( String str ) { } Sample Output: countNumAlphabetic("mA13Th9zB86") returns 8
Java Lab Assignment Write a method called convertToString. The method will take an array of chars...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars as an argument and it will return a String. Each index of the array will be part of the each index of the String. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for *...
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
Write a program that prompts the user to enter a string and displays the number of...
Write a program that prompts the user to enter a string and displays the number of characters it contains, fourth character, and last character. Note: The string may contain blanks. For example: “C++ programming is fun”. You should write a complete program.
Java Lab Assignment Write a method named findMax. The method will take an array of doubles...
Java Lab Assignment Write a method named findMax. The method will take an array of doubles as an argument, and it will return the largest value in the array. (Note, you will have to create a new array of doubles) . method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...