import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest length string is " + minLength(strings)); } } i want to stop reading from the user when the user enter "stop" either capital or small i don't want to read 50 string how can i do that
Program:
import java.util.Scanner;
public class FindMinLength
{
public static int minLength(String[] array)
{
int minLength = array[0].length();
for (int i = 0; i < array.length; i++)
{
if (array[i].length() < minLength)
minLength = array[i].length();
}
return minLength;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int c=0;
String strT="";
//Maximum 50 strings can be acccepted or less as you wanted
for (int i = 1; i<=50; i++)
{
System.out.print("Enter string " + (i) + ": ");
String str = in.nextLine();
if(str.equalsIgnoreCase("STOP"))
{
break;
}
strT=strT+str+"\n";
}
//storing all the strings into string array
String[] strings = strT.split("\n");
System.out.println("Length of smallest length string is " +
minLength(strings));
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.