0

I'm trying to create static method in Java which would return String gathered from users input from keyboard. Non static method readKey works fine but instead of creating BufferedReader I need to create instance of ATM class.

public class ATM {

    public String readKey() {
        String key = null;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            key = bufferedReader.readLine();
        } catch (IOException e) {

            e.printStackTrace();
        }
        try {
            bufferedReader.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    return key;
    }
    public static String rKey() {
        String key = null;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            key = bufferedReader.readLine();
        } catch (IOException e) {

            e.printStackTrace();
        }
        try {
            bufferedReader.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    return key;
    }



    public static void main(String[] args) {


            String cardNumber = rKey();


            String cardPin = rKey();



    }
}

While providing some input and pressing enter I receive following error:

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at packageX.ATM.rKey(ATM.java:31)
at packageX.ATM.main(ATM.java:68)

Thank you for you time, hope that my problem is well described.

2
  • 1
    Well, don't close the stream. Commented Jan 31, 2018 at 17:47
  • But I have passed the value to key String before closing the stream - right? Commented Jan 31, 2018 at 17:50

1 Answer 1

0

First, "DRY out" your code.

rKey and readKey are doing the exact same thing.


Your error happens because the first rKey() closed System.in, therefore the second one will fail because there is only one System.in, and it should not be closed throughout the lifetime of the application if you plan on continuing to use it.

You can follow this pattern for non-static uses.

class ATM {
    private Scanner sc = new Scanner(System.in);

    public String readKey() {
        String key = null;
        if (sc.hasNextLine()) {
            key = sc.readLine();
        }
        return key;
    }

    public static void main(String[] args) {
        ATM a = new ATM();

        String cardNumber = a.readKey();
        String cardPin = a.readKey();
    }
}

Non static method readKey works fine but instead of creating BufferedReader I need to create instance of ATM class

Yes, because you cannot access a non-static resource from a static context

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I understand! In public String readKey() I can close the stream, but when I'm creating static method I can't close stream because It will be closed forever. Is my understanding correct? If yes, then is it possible to close and reopen stream in static method?
@WontProvide This has nothing to do with static vs. non-static. When you wrap an InputStream in a Reader (or another InputStream), closing the the wrapper object (in this case, your BufferedReader) closes the underlying InputStream. Once you close System.in, you cannot read from it again, regardless of whether you’ve closed it in a static or non-static method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.