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 “*”.
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 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...
● 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...
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.
solution using stack Reversing a word or a sentence: Write an algorithm that will display a...
solution using stack Reversing a word or a sentence: Write an algorithm that will display a given word in a reverse order. - e.g. "Data Structures" will be "serutcurtS ataD".
Java Create a method to display a menu. When this method is called it should receive...
Java Create a method to display a menu. When this method is called it should receive the user's name, great the user, and ask them to make a selection. 1 - Change your name, 2 - Test your IQ, 3 - Display a table, 4 - Play a game, 5 - Exit.
(Display a Circle and Its Attributes) Write a program that displays a circle of random size...
(Display a Circle and Its Attributes) Write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations: diameter = 2 × radius, area = π × radius2, circumference = 2 × π × radius. Use the constant Math.PI for pi (π). All drawing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT