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
Anna35 [415]
3 years ago
5

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of strings

and two int variables, count and longest, write the code necessary to examine all the strings in the input source and determine how long the longest string (or strings are).\
Computers and Technology
2 answers:
vichka [17]3 years ago
8 0

Answer:

import java.util.Scanner;

public class LongestString

{

public static void main(String[] args) {

   

    int count = 0, longest = 0;

    Scanner input = new Scanner(System.in);

    Scanner in = new Scanner(System.in);

   

    System.out.print("Enter the size: ");

    int size = in.nextInt();

           String words[] = new String[size];    

    for(int i=0; i<size; i++) {

        System.out.print("Enter a word: ");

        words[i] = input.nextLine();

    }

   

    String longestWord = words[0];

    for(int i=0; i<size; i++) {

       

        if (words[i].length() > longestWord.length()) {

            longestWord = words[i];

            longest = words[i].length();

            count = 1;

        }

        else if (words[i].length() == longestWord.length()) {

            longest = words[i].length();

            count++;

        }

    }

 System.out.println("The longest word: " + longestWord + "\nThe length: " + longest + "\nThe count: "+ count);

}

}

Explanation:

I provided the full code, getting the inputs and print the results, in addition to your request, so that it may be more clear.

-Ask the user for the array size of <em>words</em>, holds the strings entered by the user

- Inside the first <u>for loop</u>, initialize the <em>words </em>array

<em>- </em>Inside the second <u>for loop</u>, find the longest string, its length and the count of the string.

- Print the longest string, its length and count

Usimov [2.4K]3 years ago
6 0

Answer:

// Program segment is written in Java programming language

// Comments are used for explanatory purpose

// Program segment starts here

// Initialise the two in variable to 0

count = 0;

longest =0;

// define the string variable

String myString = new String();

// Check if scanner reference contains string using while iteration

while (input.hasNext()){

// Input string while condition is true

myString = input.next();

// Compare length of string with longest variable

if (myString.length() == longest)

{

// If equal, increment count by 1

count++;

}

// If length of string is greater than longest

else

if (myString.length() > longest)

{

longest = myString.length(); // assign length of string to longest

count = 1; // set count to 1

}

}

// End of code segment

You might be interested in
Helpppppp pleaseeeeeee
Inessa05 [86]
That looks like coding.
5 0
4 years ago
JAVA
Virty [35]

Answer:

Is in the provided screenshot

Explanation:

Get the last two characters and concatenate.

6 0
3 years ago
Data about the size and composition of a population are gathered in a survey called _____.
topjm [15]
Your answer is B. :) Hope this helps!
6 0
4 years ago
Read 2 more answers
What is wrong with the formula below?<br><br> SUM(A1:A5)/SUM(B1:B5)
Vlad [161]

Answer:

You forgot the "=" sign.

Explanation:

formulas in excel start with an "="

=SUM(A1:A5)/SUM(B1:B5)

4 0
4 years ago
A program uses two classes: dog and poodle. which class is the base class and which is the derived class?
Vsevolod [243]
Dog is the base class and poodle is the derived class. 
6 0
4 years ago
Read 2 more answers
Other questions:
  • What is the first stage of perception
    6·2 answers
  • The fossilizing process that replaces organic matter with silica or calcite is:
    13·2 answers
  • The syntax used for referencing cells with their worksheet names is the sheet name, followed by ____, then the usual column lett
    8·1 answer
  • In which of these scenarios would the top 10 option be most helpful
    10·1 answer
  • Framing can create which of the following in a photograph? Mystery Saturation Aperture All of the above
    10·2 answers
  • Extending the web server functionality implies that the web server and the web-to-database middleware will properly communicate
    10·1 answer
  • TCP is a Transport-layer protocol used to communicate between applications, but it's a lightweight Transport-layer protocol, mea
    10·1 answer
  • Once the human body stops moving in a crash, the internal organs stop as well. True or false?
    11·1 answer
  • Can somebody please help me with these few questions?
    11·1 answer
  • What does a computer monitor look like when struck really hard?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!