Question

In: Computer Science

write a java prog that include an array of strings of size 50    String[] strings...

write a java prog that include an array of strings of size 50
   String[] strings = new String[50];
then find the max length of the inputs inside a method
keep reading from the user until user enters keyword to stop

input :
may ala
jony ram
asd fgghff
daniel
jwana

output :
max length : 10

Solutions

Expert Solution

CODE IN JAVA :

import java.util.*;
public class Main
{
public static int find_max_length(String[] strings,int size) // method to find maximum length
{
int max_length = 0; // initialised to 0
for(int i = 0; i < size; i++)
{
if(max_length < strings[i].length()) // updating if required
{
max_length = strings[i].length();
}
}
return max_length;
}
   public static void main(String[] args) {
       String[] strings = new String[50];
       int i = 0;
       System.out.println("Enter string: ");
       Scanner sc = new Scanner(System.in);
       strings[i] = sc.nextLine();
       // Input string until you enter "stop" using for loop
       for(i = 1; i < 50 && !strings[i-1].equals("stop"); i++)
       {
       strings[i] = sc.nextLine();
       }
       int maxLength = find_max_length(strings,i); // function called
       System.out.println("Max length: " + maxLength); // print the length
   }
}

INPUT SNIPPET :

OUTPUT SNIPPET :

Hope this resolves your doubt.

Please give an upvote if you liked my solution. Thank you :)


Related Solutions

Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Sort a string array by frequency given an array of string write a function that will...
Sort a string array by frequency given an array of string write a function that will return an array that is sorted by frequency example {"hello","hola","hello","hello","ciao","hola","hola","hola"} returned array should be {"hola","hello","ciao"}
The split method in the String class returns an array of strings consisting of the substrings...
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters. public static String[] split(String s, String regex) For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, ?, b, ?,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT