Answer to Question #305730 in Java | JSP | JSF for gunna_b

Question #305730

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


1
Expert's answer
2022-03-05T17:14:18-0500
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"); 
            
        }
      


  }


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS