0

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)
    }
}
3
  • 9
    Why don't you add your computer to run and test your code instead of asking us? Commented Mar 13, 2015 at 7:48
  • Here you have 6 scores per person, so I think following code will work. Commented Mar 13, 2015 at 8:17
  • I don't see where participants is defined/allocated, and there seems to be a missing semicolon, and what does assignScore do? Commented Mar 13, 2015 at 8:42

2 Answers 2

2

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++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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)}
  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.