Home java filling the array rows java

filling the array rows java

Author

Date

Category

The question consists of 2 parts:

Part 1:
You need to create a list of strings (for example, you need to enter 5 strings), entering from the keyboard.
Then to compare the length of each line and determine which will meet earlier: the string with the highest number of characters or the smallest. After comparison, withdraw this string.

Part 2:
Is it possible to use the fragment below for writing code?

bufferedreader reader = new buffereder (new inputstreamReader (System.in));
  System.Out.printLN ("Enter any 5 lines:");
  String Numa = Reader.ReadLine ();

Thank you in advance for the answer.


Answer 1

import java.io.bufferedReader;
Import java.io.ioException;
Import java.io.InputStreamReader;
Import java.util.arrays;
Public Class Solution {
  Public Static Void Main (String [] Args) Throws IoException {
    BufferedReader Reader = New BufferedReader (new inputStreamReader (System.in));
    System.Out.printLN ("Enter any 5 lines:");
    String Str1 = Reader.ReadLine (); // Get the first string entered from the keyboard
    String Str2 = Reader.ReadLine ();
    String Str3 = Reader.ReadLine ();
    String Str4 = Reader.ReadLine ();
    String Str5 = Reader.ReadLine (); // Get the fifth string entered from the keyboard
    STRING [] ArrayStr = {STR1, STR2, STR3, STR4, STR5}; // write the entered strings into an array
    int [] array = new int [] {STR1.LENGTH (), str2.length (), str3.length (), str4.length (), str5.length ()}; // Create an array with string lengths
    Array.sort (array); // Sort the array (do not forget to import: import java.util.arrays;)
    for (int i = 0; i & lt; arraystr.length; i ++) {// We pass by the array of entered strings
      if (ArrayStr [i] .length () == Array [0] || ArrayStr [i] .length () == Array [Array.length - 1]) {// If the length of the string is maximum or minimal, then output Her and complete the cycle
        System.Out.printLN (ArrayStr [i]);
        Break;
      }
    }
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions