dimanche 1 décembre 2019

Scanner: can't pass an automated test because of trailing output (pictures)

I have a program that uses Scanner to get user input. I then submit said program to a website that uses tests to check the correctness of my code. However despite getting correct output during my own manual runs, I run into problems with those automated tests:

I have this code:

Main.main(String[] args):

...
        Player player1 = new Player(1);
        Player player2 = new Player(2);
        player1.setName(scanner);
        player2.setName(scanner);

        System.out.println("Enter integer seed (or 0 for none):");
        int seed = Integer.valueOf(scanner.nextLine());
        Random random;
        if (seed==0)  {
            random = new Random();
        }
        else  {
            random = new Random(seed);
        }
        System.out.println();
...

Player:

    public void setName(Scanner scanner)  {
        System.out.println("First name of player " + playerNum + "?");
        this.name = scanner.nextLine();
    }

This is the error I have, the tests highlights the incorrect output:

enter image description here

It seems like it doesn't like the trailing symbols of the Enter integer seed (or 0 for none):. However, if I remove System.out.println() the test would ask for a new line. The correct output also has a new line there.

Also if I use scanner to get int like this:

...
        System.out.println("Enter integer seed (or 0 for none):");
        int seed = scanner.nextInt();
        scanner.nextLine();
        Random random;
...

I would get an error from autotests:

enter image description here

enter image description here

While when running on my own, I have no errors.

Maybe it has to do with how they give the input to the program? Maybe because they give all of the input at once? I tried to get all of the input at first input prompting (earlier in the program when creating a board), but it would end up with me having to press new line (Enter) to let Enter integer seed (or 0 for none): know there's already the number.

Aucun commentaire:

Enregistrer un commentaire