Reputation: 2499
I have an array of Objects, all objects of the class 'Person' which contains attributes - String name and Array scores. I am given a text file containing the scores of a each person. Each score is written on a new line as a String. every person will have the same amount of scores and the scores will be recorded in the same order as the Array of people. First score belongs to first person, second score to second person etc, this will repeat until or scores are recorded. There are 10 people and 6 scores per person. What i'm wondering is, can you continuously loop through the array of People objects, adding each score to the relevant persons score array?
My code
while (scanner.hasNextLine()) {
for (int i = 0; i < 10; i++) {
String NextScore = theScanner.nextLine();
participants[i].assignScore(NextScore)
}
}
Upvotes: 0
Views: 1663
Reputation: 1
That should do the trick
while (scanner.hasNextLine()) {
for (int i = 0; i < 10; i++){
for(int j = 0; j<6; j++){
String nextScore = theScanner.nextLine();
participants[i].assignScore(nextScore)}
}
}
Upvotes: 0
Reputation: 1082
Here you have 6 scores per person, so I think following code will work.
int count = 0;
int index = 0;
while(scanner.hasNextLine() {
count++;
String NextScore = theScanner.nextLine();
participants[index].assignScore(NextScore);
if(count == 6)
{
count = 0;
index++;
}
}
Upvotes: 2