1

I am currently working on a program that requests input for the names and scores of two teams. When I request input for the name and 9 scores of the first team, the scanner accepts input just fine. However, after the for loop, the scanner does not accept input for the name of the second team. This is not the entire program, but I have included all the code up until the point where it is giving me trouble. I suspect that it may have something to do with the for loop because team2 accepts user input just fine when I place it before the for loop.

import java.util.Scanner;
public class sportsGame{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String team1;
        String team2;
        int team1Scores[] = new int[9]
        int team1Total = 0;
        int team2Scores[] = new int[9];
        int team2Total = 0;

        System.out.print("Pick a name for the first team: ");
        team1 = input.nextLine();

        System.out.print("Enter a score for each of the 9 innings for the "
                + team1 + " separated by spaces: ");
        for(int i = 0; i < team1Scores.length; i++){
            team1Scores[i] = input.nextInt();
            team1Total += team1Scores[i];
        }
        System.out.print("Pick a name for the second team: ");
        team2 = input.nextLine();
    }
}
maba
  • 47,113
  • 10
  • 108
  • 118
shady
  • 73
  • 1
  • 4
  • 8
    **Related question:** [Scanner issue when using nextLine after nextInt](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextint) – Eng.Fouad Oct 10 '12 at 14:18
  • You are assuming that the name is on the same line as the scores at the end. If you want the name on the next line add `input.nextLine();` after the loop. – Peter Lawrey Oct 10 '12 at 14:20
  • Haha, nice catch I have never even thought about this. Note to OP: This is where a simply print debug statement would have shown you exactly the issue. – thatidiotguy Oct 10 '12 at 14:20
  • 2
    You are missing a semicolon after `int team1Scores[] = new int[9]` just FYI. – Reid Mac Oct 10 '12 at 14:21
  • asked the same question myself a few days ago, got two votes now with the answer. feels great :) – Victor Mukherjee Oct 10 '12 at 14:23
  • @VictorMukherjee [That's where](http://stackoverflow.com/questions/12762316/scanner-not-letting-to-accept-input-for-the-last-run-of-a-loop#comment17245345_12762316) I posted the same comment that I posted again on this question :D – Eng.Fouad Oct 10 '12 at 14:25

1 Answers1

5

Scanner's nextInt method does not skip a line, only fetches the int. So when the first loop ends, there is still one newline character left and your input.nextLine() returns a blank string. add a input.nextLine() after your loop to skip this blank line and to solve your problem like this:

for(int i = 0; i < team1Scores.length; i++){
    team1Scores[i] = input.nextInt();
    team1Total += team1Scores[i];
    }
input.nextLine();
//rest of your code
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97