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
Many computer programs contain ____ that detail the specific instructions a computer should perform (in a specific order) to car
nikklg [1K]

Answer:

Algorithms.

Explanation:

Computer system is an electronic machine that receives input data, processes or executes these data and outputs the results. It comprises of two parts, the hardware and software components.

The computer receives data created by a process and inputted by individuals. The software components are the operating system and the application softwares running in the operating system. The application software follows a well defined stages of task to execute a task. This is called algorithm.

The application uses the algorithm to execute the task on the input data to give the required result.

8 0
3 years ago
Which tab should you click if you want to access the Show All Comments option in a worksheet?
Lostsunrise [7]

Answer: Review

Because you want to review the comments

3 0
3 years ago
Read 2 more answers
Favorite friends character and why GO!
Masteriza [31]
Rachel Def i really don’t know just cause
7 0
3 years ago
Read 2 more answers
Id is 960 1788 6557 <br><br> pass is nick2020 <br><br> anybody can join
EleoNora [17]

Answer:

what is this a zoom a kahoot or what

3 0
3 years ago
___________ are the constant values that are used in a program. ​
77julia77 [94]

Answer: Data

Explanation:

3 0
3 years ago
Other questions:
  • Branching is so called because <br>​
    5·2 answers
  • Which is a network of devices built around a person, typically within 10 meters of range?
    5·1 answer
  • What are some ways social media networks make money from users
    10·1 answer
  • I don't understand question how do I work it out?
    7·2 answers
  • List 7 basic internal component found in a computer tower?
    11·2 answers
  • this device can be used to type documents,send email,browse the internet,handle spreadsheets,do presentations,play games,and mor
    14·1 answer
  • The advantage of using a belt drive over chain drive. vice versa
    12·1 answer
  • Portable Document Format is a proprietary document file type created by Adobe Systems that is compatible with most computer syst
    7·1 answer
  • PLEASE HELP THIS IS DUE TODAY!! WILL MARK BRAINLIEST!!
    13·2 answers
  • Which is not and input device , keyboard , joystick, Monitor, Microphone​
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!