Is there a way for me to use the same scanner for System.in input and FileInputStream input?

Is there a way to use the same scanner for the System.in input and the FileInputStream input?

This is how I initialized the scanner in my main class:

public class Nimsys {
    public static Scanner keyboardIn;

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

      

In the main Nimsys class, this is how I get the input:

String inputString1 = keyboardIn.nextLine();

      

In another class, this is how I use the Nimsys scanner:

int inputInt1 = Nimsys.keyboardIn.nextInt();

      

But now in my main Nimsys class, I am trying to scan the whole file - so far I have used a different scanner, as you can see in the code below. However, can all of this be done with 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]);
        }

      

Thank you so much!

Tom

+3


source to share


3 answers


If I understand your question (not that I think a global variable is a great solution), you can change (and possibly 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 try-catch

from



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

      

and change it to something like

inputStream = Nimsys.keyboardIn;

      

(or replace inputStream

withNimsys.keyboardIn

rather than prescriptive, but perhaps rename Nimsys.keyboardIn

toNimsys.in

). Hopefully you are using an IDE that supports refactoring.

+1


source


I would recommend that you use the same scanner for multiple sources . From what I can tell from the code you described, you won't get anything. In general, one scanner should represent one data source.

However, if you are in the mood for this idea, you can write your own implementation InputStream

that combines inputs from System.in

and yours FileInputStream

. For ideas on how to do this, see this related question . Then build a scanner with an instance of your two sources InputStream

.



This raises many other questions: How exactly do you intend to properly combine input from the two sources? The content of the file will be available as soon as the file is opened. The login from System.in

will be available when the user types it. How does your combo Scanner

choose what to output and when? These are the questions you will have to answer if you decide to write your own InputStream

to wrap up two sources.

0


source


No, you cannot do this. The scanner is just a wrapper class, which means the actual stream sources you are using are FileInputStream and system.in, obviously you cannot do that, and you cannot take advantage of this.

0


source







All Articles