Given a positive number, create an array with that number as a size and populate it with the items/names provided. Additionally, the program should print out the longest name in the array. Sample Run1 3 Apple Banana Kiwi Output1: Banana is the longest name
public class MyClass{
public static void main(String args[]) {
String[] split = args[0].split("\\s+");
int size = Integer.parseInt(split[0]);
if(size > 0 & size <= split.length){
//create new mass of names
String[] mass = new String[size];
for(int i = 0; i<size; i++){
mass[i] = split[i+1];
}
int max = 0;
int position = 0;
//determining the longest name
for(int i = 1; i<size; i++){
if(max < mass[i].length()){
max = mass[i].length();
position = i;
}
}
System.out.println(mass[position] + " is the longest name");
}
}
}
Comments
Leave a comment