0

I am using a scanner to populate an array with data. It seems to work because when I print (player[i].Name) in the "if" statement it prints a value. But when I try to print this value outside of the statement (players[1].Name), it gives a null value.

int i = 0;
        while (scanner.hasNextLine()) {
                if (scanner.hasNext()) {
                players[i].Name = scanner.next();
                System.out.println(players[i].Name);
                }
                if (scanner.hasNext()) {
                players[i].Position = scanner.next();
                System.out.println(players[i].Position);
                }
                if (scanner.hasNext()) {
                players[i].Height = scanner.next();
                System.out.println(players[i].Height);
                }
                 if (scanner.hasNext()) {
                players[i].Weight = scanner.next();
                System.out.println(players[i].Weight);
                }
               i++;      
            }//end while
3
  • 1
    As written, the scanner will put all of the players into players[0], then the loop will no-op 9 times for i 1->9. Is that also the case in the complete code? Commented May 14, 2012 at 3:37
  • Are you sure the statement scanner.next() returns some value for the last iteratio.. it might be getting overwritten by null... Commented May 14, 2012 at 3:40
  • did you try only while loop which you have written inside the for loop...I think, No need to put in for loop... Commented May 14, 2012 at 3:42

2 Answers 2

1

You're doing this 10x (for each i):

 // Get all possible scanner entries for all possible lines for i=0
 while (scanner.hasNextLine()) {
        if (scanner.hasNext()) {
            players[i].Name = scanner.next();
 // .. then repeat getting all scanner entries for all lines for i=1...

Maybe this is what you meant:

int i = 0;
while (scanner.hasNextLine()) {
    if (scanner.hasNext()) {
        players[i].Name = scanner.next();
        System.out.println(players[i].Name);
        i++;
    }
}

Or maybe even this:

int i = 0;
while (scanner.hasNextLine()) {
    while (scanner.hasNext()) {
        players[i].Name = scanner.next();
        System.out.println(players[i].Name);
        i++;
    }
}

Or this:

int i = 0;
while ((i < 10) && scanner.hasNextLine()) {
    while ((i < 10) && scanner.hasNext()) {
        players[i].Name = scanner.next();
        System.out.println(players[i].Name);
        i++;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your comments. I tried your solution but may have missed something. I have posted a more complete code sample so maybe you have a better idea of what I am trying to accomplish
Now, the player[0] values are correct but player[1] is off by 1 and player[2] is off by 2... Im just not sure what Im missing here
SUGGESTIONS: Please lose the "for" loop. Increment i++ inside your inner while loop. Change your "println" as follows: System.out.println ("players[" + i + "]=" + players[i].Name);
1

Your doing this in a for loop. I don't think that will yeild the desired result. Essentially, you're assigning the name to the same player while scanner has next. Take it out of the for loop and just have the while loop with i++ as the last but of the loop.

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.