Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Multiple Scanners

I have a class that creates multiple Integer objects and puts them into a LinkedList 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 application 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.

I'm 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 sells these shares. That is why I am using a while loop.

like image 761
Ryan Gibson Avatar asked Nov 04 '13 11:11

Ryan Gibson


People also ask

Can you have multiple scanners in Java?

You can create only one scanner object and use it any where else in this class.

Do I only need one Scanner in Java?

You should only create one Scanner per input stream. Among other things, the scanner reads ahead and so will consume more of the input than has actually been returned.

What is nextInt () in Java?

The nextInt() method scans the next token of the input data as an “int”. As the name of the class Scanner elaborates, nextInt() method of this class is used to scan or parse the input. The input can be stored either as String, read from a file, real-time data or any System input by the user.


3 Answers

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.

like image 145
Peter Lawrey Avatar answered Oct 13 '22 17:10

Peter Lawrey


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.

like image 11
nelly Avatar answered Oct 13 '22 15:10

nelly


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

I've verified it while debugging the Scanner class source code, and there 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().

like image 5
freedev Avatar answered Oct 13 '22 15:10

freedev