1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
OlgaM077 [116]
3 years ago
15

Write a Comparator that compares String objects by the number of words they contain. Consider any nonwhitespace string of charac

ters to be a word. For example, "hello" comes before "I see", which comes before "You can do it"
Computers and Technology
1 answer:
Readme [11.4K]3 years ago
8 0

Answer:

import java.util.Scanner;

public class num12 {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter the first String");

       String word1 = in.nextLine();

       System.out.println("Enter the second String");

       String word2 = in.nextLine();

       System.out.println("Enter the third String");

       String word3 = in.nextLine();

       //Remove all white spaces

        String cword1 = word1.replace(" ","");

       String cword2 = word2.replace(" ","");

       String cword3 = word3.replace(" ","");

       //Comparing the string by their lengths

       if(cword1.length()>cword2.length()&&cword1.length()>cword3.length()){

           System.out.println(word1+" Is the longest");

       }

       else if(cword2.length()>cword1.length()&&cword2.length()>cword3.length()){

           System.out.println(word2+" Is the longest");

       }

       else{

           System.out.println(cword3+" Is the longest");

       }

   }

}

Explanation:

Using Java Programming Language

Use the Scanner Class to obtain the String values from the user

Save them in different variables

Use the replace() method in java to remove white space from any of the string entered

Using if and else statements compare the lengths of the strings (word.length()) returns the length of the word.

Print out the word that is longest

NOTE I have compared three Strings, comparing two would have been more straigth forward

You might be interested in
PLEASE WRITE A ONE SENTANCE SUMMARY, WILL GIVE BRAINLIEST!! (history of computers) You have completed an extensive investigation
vladimir1956 [14]

Answer:

Charles Babbage, (born December 26, 1791, London, England—died October 18, 1871, London), English mathematician and inventor who is credited with having conceived the first automatic digital computer.

Explanation:

The idea of mechanically calculating mathematical tables first came to Babbage in 1812 or 1813. Later he made a small calculator that could perform certain mathematical computations to eight decimals. Then in 1823 he obtained government support for the design of a projected machine, the Difference Engine, with a 20-decimal capacity. The Difference Engine was a digital device: it operated on discrete digits rather than smooth quantities, and the digits were decimal (0–9), represented by positions on toothed wheels rather than binary digits (“bits”). When one of the toothed wheels turned from nine to zero, it caused the next wheel to advance one position, carrying the digit. Like modern computers, the Difference Engine had storage—that is, a place where data could be held temporarily for later processing.

6 0
3 years ago
Write the code to declare a variable to hold the value of the grade you hope to get in this class. What stdio.h input function w
GREYUIT [131]

Answer:

// code to read grade

#include <stdio.h>

// main function

int main(void) {

   // if grade is character

char grade;

// if grade is numeric then we can use int or double

// int grade;

// double grade;

printf("Enter your grade:");

// read grade from user

scanf("%c",&grade);

// print grade

printf("your grade is:%c",grade);

return 0;

}

Explanation:

To read a value, scanf() function is used from stdio.h.Read a grade from user and assign it to variable "grade".

Output:

Enter your grade:A

your grade is:A

// code to read die volt

#include <stdio.h>

// main function

int main(void) {

// variable

double die_volt;

printf("Enter die volt:");

// read die volt from user

scanf("%lf",&die_volt);

// print die volt

printf("Entered die volt is:%0.2lf",die_volt);

return 0;

}

Explanation:

Read the die volt from user and assign it to variable "die_volt" with the help

of scanf() function.

Output:

Enter die volt:220                                                                                                        

Entered die volt is:220.00

4 0
3 years ago
___ is the branch of computer science that explores techniques for incorporating aspects of intelligence into computer systems.
mamaluj [8]
Artificial Intelligence
5 0
3 years ago
Top 5 best comedy or action movies.
Cloud [144]
Comedy movies you should watch is
grown ups
spider man into the spider verse
action movies you can do
like a fast and furious movie ?
some cool scary movies are
the strangers
insidious
and the serbian film :)
7 0
3 years ago
Read 2 more answers
I need help with this please and thanks
yuradex [85]

WHOA, ok thats ALOT but i can tell you that 9 is C.

3 0
3 years ago
Other questions:
  • Which of the following best defines natural selection?
    8·1 answer
  • Select the answers that best describe showing respect for confidential data. Check all of the boxes that
    10·1 answer
  • What is database? Please answer ASAP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ON Microsoft power
    10·2 answers
  • Which careers have the highest minimum experience requirement?
    14·1 answer
  • Rachel needs to include a new organizational chart in her project. Which type of illustration should she use?: *
    11·1 answer
  • Which statement assigns the value 98 to the variable myScore in Python?
    14·2 answers
  • The Federal Trade Commission FIP principle of Notice/Awareness states that
    5·1 answer
  • When performing the ipconfig command, what does the following output line depict if found in the tunnel adapter settings?
    5·1 answer
  • If a folder exists on an NTFS partition, which permission is needed by a user who needs to set security permissions on the folde
    11·1 answer
  • Write Python code that prompts the user to guess Heads or Tails. Your code will continue to display the results and prompt the u
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!