0

I'm trying to create a simple application that takes in a certain number of names and creates tournament brackets from them. Right now I'm stuck creating the number of matches and contestants per match. As it stands, I want to create a failsafe that will warn the user if the number of people they want in a match does not divide evenly with the total number of people. I ask them to simply enter either 'Y' or 'N' to dictate this. The problem is that the program does not give them the chance to enter their response and automatically spits out a NoSuchElementFound exception: No line found.

This is a brand new scanner made to take in this response. I tried using .hasNext in order to see if there is no next line, and the result it gets is false. Essentially, after I've gotten all of the contestants (a separate method that works fine) this is the console:

Total Number of Contestants: 5

How many contestants do you want to compete per match?

2

Warning: Choosing this number means that there will not be the same number of contestants in each match.

Reminder that the total number of contestants is: 5

If you want to proceed, enter 'Y'. If you want to enter a different number of contestants per match, enter 'N'.

check value: false

If it helps, this is the troublesome code:

    String answer = "yes";

    Scanner scan = new Scanner(System.in);      
    float roundedMatchCount = 0;

    float matchCount = 0;
    entrant victor = new entrant();

    float matchMembers;

    Scanner scan = new Scanner(System.in);

    System.out.println("How many contestants do you want to compete per match?");
    matchMembers = scan.nextInt();



    matchCount = input.size() / matchMembers;



    scan.close();

    Scanner s = new Scanner(System.in);


    if (input.size() % matchMembers !=0)
        //if the number of contestants is not divisble by the number 
        //of participants in a match
    {
        System.out.println("Warning: Choosing this number means that there will not be the same number of contestants in each match.");
        System.out.println("Reminder that the total number of contestants is: "
                +input.size());
        //begin while


            System.out.println("If you want to proceed, enter 'Y'. If you want to enter a different number of contestants per match, enter 'N'.");

            boolean check = s.hasNextLine();

            System.out.println("check value: " + check);

            answer = s.nextLine();
            //WHAT

            if (answer.contains("N"))
            {
                //ask for a new number

            }

            else if (answer.contains("Y"))
            {
              //round the number of matches up or down depending on user input
            }

            else
            {
                System.out.println("Error: Invalid response.");

            }


    }

Please note that input is an ArrayList that was passed into this method from the previous one. I know that the correct number of entries is inside it because in previous tests I had its contents and size printed out.

Maria-Andersado
  • 777
  • 1
  • 9
  • 18
  • Try getting rid of the `boolean check` line and the print statement after it. `answer = s.nextLine()` should be all you need. – Zarwan Sep 04 '15 at 03:45
  • That was what I was doing before I did the .hasNextLine() check. Same error, .hasNextLine() was added as part of the debugging process. – Maria-Andersado Sep 04 '15 at 03:53
  • Alright, but you should still get rid of it. Also get rid of `scan.close()`; that is closing your input stream, `System.in`, as well. – Zarwan Sep 04 '15 at 03:56
  • This was also something I had before. I was still getting the same error. – Maria-Andersado Sep 04 '15 at 03:58
  • Why are you calling Scanner scan = new Scanner(System.in); at the third line and then again at the 11th? – user5292387 Sep 04 '15 at 04:00
  • I think you should try them again (in combination) and also delete your second declaration of `scan` like @user5292387 pointed out. – Zarwan Sep 04 '15 at 04:04
  • I took this code out of a much larger program, so I'm sorry for the typos. In my original code, I use a scanner to get an int (the number of people in a match) and since I know going from ints to strings can cause problems, I close it and create a new one that's going to get a string. In my original code, I can guarantee you that I simply create a new scanner, get an integer, close the scanner, create another scanner, try to get a string, but it won't let me. – Maria-Andersado Sep 04 '15 at 04:07
  • You don't need two scanners for different data types, you can just use `nextInt()` or use `nextLine()` and cast it to an int. – Zarwan Sep 04 '15 at 04:09
  • 1
    This is a duplicate of http://stackoverflow.com/questions/15443383/scanner-nosuchelementexception. Do not close scanner. – Dakshinamurthy Karra Sep 04 '15 at 04:16
  • This did the trick, thank you! – Maria-Andersado Sep 07 '15 at 20:47

1 Answers1

0

As KDM helpfully pointed out, my issue was being caused by how I closed the previous scanner. Getting ride of scan.close() solved my problem.

Community
  • 1
  • 1
Maria-Andersado
  • 777
  • 1
  • 9
  • 18