Question

In: Computer Science

Display the shortest words in a sentence in JAVA Write a method that displays all the...

Display the shortest words in a sentence in JAVA

Write a method that displays all the shortest words in a given sentence. You are not allowed to use array

In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence.

You must design the algorithms for both the programmer-defined method and the main method.

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


class Main{

    static void displayShortest(String s){
        //Split the sentence
        String words[] = s.split(" ");
        //Initialize a new list to store shortest words
        List<String> shortest = new ArrayList<>();
        //Initialize shortest words list
        int sl = Integer.MAX_VALUE;
        //Loop through all words
        for(String word : words){
            //If length of the word is lower than shortest length obtained so far
            if(word.length()<sl){
                //Create a new list and add
                shortest = new ArrayList<>();
                sl = word.length();
                shortest.add(word);
            }
            //If word length is same as the shortest length obtained, add to list
            else if(word.length()==sl){
                shortest.add(word);
            }
        }
        //Print to console
        System.out.println("Shortest words are : "+shortest);
    }

    public static void main(String[] args) {
        System.out.println("Enter a sentence");
        Scanner obj = new Scanner(System.in);
        String s = obj.nextLine();
        displayShortest(s);
    }
}

Related Solutions

Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
Using python Write a program that displays all of states in the U.S. and display each...
Using python Write a program that displays all of states in the U.S. and display each state that begins with the letter A.
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Java Write a program that displays all the numbers from 100 to 200 that are divisible...
Java Write a program that displays all the numbers from 100 to 200 that are divisible by 5 or 6, but not both Make sure all instructions and outputs for the user are explicit
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a...
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a title Ask the user to enter the following values: The type of plane(Boeing 747 or Airbus A300) The number of passengers on board the Plane The number of crew aboard the plane The maximum number of people that can be carried by one liferaft assuming all the liferafts on the plane are the same size The actual number of liferafts that are available on...
Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending...
Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending in 3. • Write a for loop to display a string entered by the user backwards.
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT