Naim Kabir Naim Kabir - 1 month ago 8
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 = "";
part.

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

Answer

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.