Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:20:04+00:00 2026-06-14T08:20:04+00:00

I have two blocks of code here. One scanner properly waits user input, and

  • 0

I have two blocks of code here. One scanner properly waits user input, and the other just blows right through it and calls nextInt() which returns a NoSuchElementException. Here is the block that works:

public void startGame() {
   out.println("Player1: 1 for dumb player, 2 for smart player, 3 for human player.");
   Scanner scan = new Scanner(System.in);
   p = scan.nextInt();

   if (p == 1)
        p1 = new DumbPlayer("ONE");
   if (p == 2)
        p1 = new SmartPlayer("ONE");
   else 
       p1 = new HumanPlayer("ONE");

   out.println("Player2: 1 for dumb player, 2 for smart player, 3 for human player.");
   p = scan.nextInt();

   if (p == 1)
        p2 = new DumbPlayer("TWO");
   if (p == 2)
        p2 = new SmartPlayer("TWO");
   else 
        p2 = new HumanPlayer("TWO");

   scan.close();

And here is the block that does not:

public int findBestMove(Set<Integer> moves, Board b) {

    Set<Integer> set = new HashSet<Integer>();

    out.println("Player " +name+ ", select a column from 1-7: ");
    Scanner scan = new Scanner(System.in);  <--here it should wait for input, but does not!
    int move = scan.nextInt();   <-- NoSuchElementException
    scan.close();

    for (int x = 1; x <= 7; x++) {
        set.add(move);
        move += 7;
    }
    ....etc

Both of these are separate classes, and are called from a main method in yet another class. Basically main() calls startGame(), which in turn calls the findBestMove() method of some Player class…which is where the non-working code resides. Are there times in the program where it is not appropriate to take input? I was under the impression that anytime I wanted user input, I could use this approach. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:20:05+00:00Added an answer on June 14, 2026 at 8:20 am

    According to the java.util.Scanner javadoc, Scanner.close() closes the associated stream if this stream implements the Closeable interface. java.lang.System.in is an InputStream, which implements the Closeable interface. Hence, after calling Scanner.close() on a Scanner which is associated with System.in, the System.in stream is closed and not available anymore.

    The following SSCCE works for me. I have removed some code from the question which is not relevant to the actual issue. Note that with this approach, while it works, Eclipse gives me the warning "Resource leak: 'scan' is never closed", so a better solution would be to use one Scanner instance only.

    package com.example;
    
    import java.util.Scanner;
    import java.util.Set;
    import static java.lang.System.out;
    
    public class ScannerTest {
        int p = 0;
        String name = "Test";
    
        public void startGame() {
            out.println("Player1: 1 for dumb player, 2 for smart player, 3 for human player.");
            Scanner scan = new Scanner(System.in);
            p = scan.nextInt();
    
            out.println("Result1: " + p);
    
            out.println("Player2: 1 for dumb player, 2 for smart player, 3 for human player.");
            p = scan.nextInt();
    
            out.println("Result2: " + p);
    
            // scan.close();   // Do not close the Scanner to leave System.in open
        }
    
        public int findBestMove(Set<Integer> moves, Object /*Board*/ b) {
            out.println("Player " +name+ ", select a column from 1-7: ");
            Scanner scan = new Scanner(System.in);
            int move = scan.nextInt();
            // scan.close();   // Do not close the Scanner to leave System.in open
    
            out.println("Move: " + move);
    
            return 0;
        }
    
        public void run() {
            startGame();
            findBestMove(null, null);
        }
    
        public static void main(String[] args) {
            ScannerTest st = new ScannerTest();
            st.run();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two code blocks. Code block 1 var checkboxes = $(div.c1
I have two code examples below. One works, but the second (using extend to
Here is my problem. I use blocks to display two labels on screen one
I have the same block of code repeated with two different variables. This block
I have two blocks in HTML5 canvas. Blue Block ie fixed in the canvas
I have my main stage, and I have two objects (blocks), these two objects
I have these div blocks called columns where each column div contains separate two
I have two block (like Block1 and Block2) and I have to display one
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
So I'm designing a myspace page and I have two images, one is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.