You should not have multiple scanners if you want the format published in your question, you can use regular expressions for this.
This demonstrates a regular expression to match the player and use as a separator for the scanner. I gave the scanner a line in my example, but the method is the same, regardless of the source.
int count = 0; Pattern playerPattern = Pattern.compile("\\w+\\s\\w+(?:,\\w){1,3}"); Scanner fileScan = new Scanner("Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s Jill Jenks,o,o,s,h,h,o,o Will Jones,o,o,w,h,o,o,o,o,w,o,o"); fileScan.useDelimiter("(?<=,\\w)\\s"); while (fileScan.hasNext()){ String player = fileScan.next(); Matcher m = playerPattern.matcher(player); if (m.find()) { player = m.group(0); } else { throw new InputMismatchException("Players data not in expected format on string: " + player); } System.out.println(player); count++; } System.out.printf("%d players found.", count);
Exit:
Sam Slugger,h,h,o Jill Jenks,o,o,s Will Jones,o,o,w
The call to Scanner.delimiter() sets the delimiter for retrieving tokens. Regular expression (?<=,\\w)\\s :
(?< // positive lookbehind ,\w // literal comma, word character ) \s // whitespace character
Which limits the players to a space between their records, not matching anything but this space, and does not correspond to a space between names.
The regular expression used to extract up to 3 pieces per player is \\w+\\s\\w+(?:,\\w){1,3} :
\w+ // matches one to unlimited word characters (?: // begin non-capturing group ,\w // literal comma, word character ){1,3} // match non-capturing group 1 - 3 times