I strongly suggest you use String#split
which returns an array. Since you have a different number of spaces between the name and first number, you can split by multiple spaces using split("\\s+")
. For example
String line = "John Smith 92 80 86 76 95";
String[] tokens = line.split("\\s+");
The split
will return
tokens = { "John", "Smith", "92", "80", "76", "95" };
The first two indices make the name and parse the rest of the indices. Since every line has the same number of tokens, it should work fine for you in a loop.
String firstName = tokens[0];
String lastName = tokens[1];
int hw = Integer.parseInt(tokens[2]);
...
Note see @janos comment below for another option for the same functionality