String name= input.next();
String gend= input.next();
This seems to be an issue (especially if you're on the last line). You've already read the entire line, so why read any further from input
? What if there's nothing else to read? Just split()
line
on spaces and extract the first two elements as name
and gend
:
String[] split = line.split("\\s+", 3);
String name = split[0];
String gend = split[1];
Note that the second argument of split()
indicates that the string should only be split into a maximum of 3 pieces (which is optimal, since we only want the first two elements of the resulting array).