Java Multiple Scanners

I have a class that creates multiple integer objects onto a linked list as shown below:

public class Shares<E> implements Queue<E> {
protected LinkedList<E> L;


public Shares() {
    L = new LinkedList<E>();
}

public boolean add(E price) {
    System.out.println("How many of these shares would you like?");
    Scanner scanInt;
    scanInt = new Scanner(System.in);
    Integer noShares = scanInt.nextInt();
    for (int i = 0; i < noShares; i++) {
        L.addLast(price);
    }
    scanInt.close();

    return true;
}

I have an application that scans for the input "add" from the console and if found invokes the method add as shown below:

public class Application {
private static Scanner scan;

public static <E> void main(String[] args) {
    Queue<Integer> S = new Shares<Integer>();
    scan = new Scanner(System.in);
    System.out.println("Please type add");
    String sentence = scan.nextLine();
    while (sentence.equals("quit") == false) {
        if (sentence.equals("add")) {

            System.out
                    .println("What price would you like to buy your shares at?");

            S.add((Integer) scan.nextInt());

        } else
            System.exit(0);

        sentence = scan.nextLine();
    }
}

}

The appliation should allow the user to enter "add" as many times as they wish but the error "no line found" appears after the add method has been invoked.

Im guessing this is because the scanner in the method has not been closed and then reopened when needed. Is this what is wrong with the program and if so how would I go about fixing it?

Please note this program is not finished as I will be adding a selling method that sell these shares that is why I am using a while loop.


Having multiple wrappers for any stream is a great way to really confuse yourself. I suggest you only ever wrap a stream once unless you really know what you are doing.

The simplest way to do this is to use a singleton in this case as it wraps another singleton (the best is to pass around the Scanner as an argument)

public class Application { 
    // use this Scanner in all you other code, don't create another one.
    static final Scanner scan = new Scanner(System.in);

    public static <E> void main(String[] args) {

Im guessing this is because the scanner in the method has not been closed

Once you close a stream it closes the underlying stream and you can't use it again. Only close System.in if you want to prevent it being used again.

how would I go about fixing it?

The best solution is to have all your Scanner use in one place, one method or one class. You have your main() do all the interaction with the user and pass the values to your data structure. Having objects which initialise themselves is a bad practice to get into and if you start doing this, it will plague you for the rest of your development days ;) (Seriously you will see this done again and again and its often a nightmare)


BTW Never exit a program without explanation. Calling System.exit(0); without even an error message is also a nightmare. I once worked on a project which has 260 calls to System.exit() often without an error message, you can imagine how much fun it is to diagnose a server just stopping for no apparent reason.


A first mistake is that this line of code

scanInt.close();

closes the System.in, not just the scanInt object. This means that after the first call to add, the scan object will only consume the input it already has and then you'll receive a NoSuchElementException: Remove this line.

Now, if you replace the last line you have with this

sentence = scan.nextLine();
System.out.println("sentence: "" + sentence + """);

you will see that the last input you get before exiting is an empty String. So in the next loop you enter the else statement and your program stops execution. You can fix this problem by adding the following:

scan.nextLine(); // consume the first always empty String...
System.out.println("Please type add");
sentence = scan.nextLine(); // and then get the actual value

However, I will agree with Peter that you should not use multiple wrappers. Consider passing the Scanner object as an argument in the Shares class contractor.


Have multiple scanners (on same stream) is a very bad practice, because scanners consume the stream they share.

I've verified while debugging the source code and looking at Scanner class I've found:

  • a reference to the source input stream
  • a internal private buffer used to hold input.
  • So when a scanner instance consume its stream, basically it just read a bunch of bytes (1024) and the stream's position is moved ahead.

    For example when the nextLine() method is invoket, behind the scenes the source.read() copy the result into the private buffer.

    Obviously the state of other Scanner becomes corrupted (invalid).

    Try to debug the Java source code yourself and/or look at the method Scanner.readInput() .

    链接地址: http://www.djcxy.com/p/96110.html

    上一篇: 如何在不关闭底层System.in的情况下关闭扫描器?

    下一篇: Java多个扫描器