0

I'm working on a program that gets a users input from a command line. However, when the program loops, it repeats some lines of code (shown below).

My problem is, for every iteration of the loop past the first one, it skips the game title and goes straight to the score.

How would I prevent this from happening?

p = System.out.print()

Code:

        for (int i = 0; i <= 10; i++) {
            if( i == 0) {
                p("Enter player: ");
                playerName = scan.nextLine();
            }           

            try {       
                gameName = "";
                score = 0;
                time = 0;

                p("Enter game: ");
                gameName = scan.nextLine();

                p("Enter score: ");
                score = scan.nextInt();

                p("Enter time: ");
                time = scan.nextInt();

                Game game = new Game(gameName, score, time);
                if (i == 0) {
                    textToPrint = playerName + "\r\n- - - - - - - - - - - - -\r\n" + game.getGame() + " : " + game.getScore() + " : " + game.getTime() + "\r\n";
                } else {
                    textToPrint = game.getGame() + " : " + game.getScore() + " : " + game.getTime() + "\r\n";
                }               
            } catch (InputMismatchException e) {
                p("You must enter an integer!");            
            }

            try {
                WriteFile data = new WriteFile("scores.txt");       
                data.WriteToFile(textToPrint);
            } catch (IOException e) {
                p("Something went wrong during printing: " + e.getMessage());

            }   
        }   

And it repeats here:

                p("Enter game: ");
                gameName = scan.nextLine();

                p("Enter score: ");
                score = scan.nextInt();

So the output looks like this:

Enter game: Enter score:

Any ideas would be greatly appreciated!

Thanks!

Henry
  • 26
  • 2

0 Answers0