tomglynch
tomglynch

Reputation: 1816

Is there a way for me to be able to use the same scanner for both a System.in Input and for a FileInputStream Input?

Is there a way for me to be able to use the same scanner for both a System.in Input and for a FileInputStream Input?

Here is how I have initialized the scanner in my main class:

public class Nimsys {
    public static Scanner keyboardIn;

    static {
        keyboardIn = new Scanner(System.in);
    } ...

In the main class Nimsys here is how I get input:

String inputString1 = keyboardIn.nextLine();

In another class here is how I use the scanner from Nimsys:

int inputInt1 = Nimsys.keyboardIn.nextInt();

But now in my main class Nimsys I am trying to scan in a whole file - so far I have used another scanner, as you can see in the code below. However, is it possible to have it all done by the original scanner?

try
        {
            inputStream = new Scanner(new FileInputStream("file.txt"));
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File morestuff.txt was not found");         
        }
        String[] reopenPlayers = new String[100];
        int i = 0;
        while(inputStream.hasNextLine()){
            reopenPlayers[i]=inputStream.nextLine();
        System.out.println(reopenPlayers[i]);
        }

Thanks a lot!

Tom

Upvotes: 3

Views: 213

Answers (3)

Elliott Frisch
Elliott Frisch

Reputation: 201477

If I understand your question (not that I think a global variable is a great solution), you could change (and perhaps rename)

keyboardIn = new Scanner(System.in);

to something like

try {
    keyboardIn = new Scanner(new FileInputStream("file.txt"));
} catch (FileNotFoundException e) {
    System.out.println("file \"file.txt\" not found");
    e.printStackTrace();
}

and then remove the try-catch from

inputStream = new Scanner(new FileInputStream("file.txt"));

and modify it to something like

inputStream = Nimsys.keyboardIn;

(or replace inputStream with Nimsys.keyboardIn and not to be prescriptive but perhaps rename Nimsys.keyboardIn to Nimsys.in). Hopefully you're using an IDE that supports refactoring.

Upvotes: 1

augray
augray

Reputation: 3161

I would recommend against you trying to use the same scanner for multiple sources. From what I can tell from the code you've described, you wouldn't gain anything by it. In general, one Scanner should represent a single source of data.

However, if you're dead-set on the idea, you can write your own implementation of InputStream which combines the input from System.in and your FileInputStream. For ideas on how to do this, see this related question. Then construct the scanner with an instance of your two-source InputStream.

This brings up a host of other questions though- how exactly do you intend to properly combine the input from the two sources? The file contents will be available as soon as the file has been opened. The input from System.in will be available as the user types it. How should your combined Scanner choose what to output and when? These are questions you would have to answer if you choose to write your own InputStream to wrap the two sources.

Upvotes: 0

Ce Zhang
Ce Zhang

Reputation: 15

No you cannot do that, Scanner is just a wrapper class, which means the actual stream sources you are using are FileInputStream and system.in, obviously you cannot do this, and there is not much benefit if you can do this.

Upvotes: 0

Related Questions