The given code throwing StringIndexOutOfBoundException exception. Someone Please help me how can I solve this exception....
public static void longestName(Scanner console, int n) {
String name = "";
String longest= "";
boolean tie = false;
for(int i=1; i<=n; i++) {
System.out.print("Enter name #" + i + ":");
name = console.next();
if(name.length( ) == longest.length( )) {
tie = true;
}
else if(name.length( ) > longest.length( ) ) {
tie = false;
}
}
// now change name to all lower case, then change the first letter
longest = longest.toLowerCase( );
longest = Character.toUpperCase (longest.charAt( 0 ) ) + longest.substring(1);
System.out.println(longest + "'s name is longest");
if(tie==true) {
System.out.println(" (There was a tie! ) " );
}
}