Naim Kabir Naim Kabir - 10 months ago 47
Java Question

What does `String longest = ""` mean and do?

This is the bottom portion of the of my program. My friend helped me out; however he didn't explain the

String longest = "";

public static void longestName (Scanner z, int x){
String longest = "";
for(int i= 1;i<=x;i++){
System.out.println("Insert Name "+i+":");
System.out.println(longest+"'s name is the longest");

Answer Source

You are declaring String longest="" and getting input name. Then if the length of the input name is longer than the String longest then the new name is set as longest.