0

I am calling the name_setter() method from the 'main' by creating an object of customer class to get the input from console and store the name entered in the 'name' variable of the object of the customer class.

import java.io.*;

public class Rental {

    public static void main(String[] args) {
        customer c = new customer();
        c.name_setter(); // calls the method from customer class
    }
}

class customer {
    String name;

    String name_setter() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.println("Enter the name:"); // executes till here
            name = br.readLine(); // stream close error
            System.out.println(name);
            if (name.length() == 0) {
                name = br.readLine();
            }
        } catch (IOException e) {
            System.out.println("" + e);
        }

        return name;
    }
}

the error i am getting is:

java.io.IOException: Stream closed
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Your code works for me, but one thing I will tell you is that you shouldn't be opening more than one stream based on System.in, and should only close it when you're completely done getting all input. – Hovercraft Full Of Eels Dec 24 '16 at 14:17
  • And I know that you're not doing this with the code above, but I suspect that it's part of a larger program, and that you might be opening and closing System.in based Streams and that's something you shouldn't do. – Hovercraft Full Of Eels Dec 24 '16 at 14:18
  • And another thing -- your program structure is broken. The class Customer (not customer, by the way -- the first letter should be upper-case), should not have user input code within it. It should have fields, such as name fields, constructor, setter and getter methods, but user input and output code should be elsewhere. – Hovercraft Full Of Eels Dec 24 '16 at 14:19
  • You have a method called setter, but it is not a true setter method in the JavaBean sense. Setter methods are often declared void, and should accept the data used to set the field as a parameter of the method. – Hovercraft Full Of Eels Dec 24 '16 at 14:30
  • @Hovercraft Full Of Eels yes i have taken just an integer input using scanner class. – ADITYA AISHWARY Dec 24 '16 at 14:34
  • @HovercraftFullOfEels I am a new java programmer. Thanks for all the information. Indeed was of great help – ADITYA AISHWARY Dec 24 '16 at 14:36

1 Answers1

1

As Hovercraft Full Of Eels said, this code doesn't cause problem at runtime.
It is probably different from which one rises the exception : java.io.IOException: Stream closed.

Beware : you chain your BufferedReader with the System.in InputStream.
When you close the BufferedReader, it closes the System.in.

For example at the end of this code System.in is closed:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.close();
br = new BufferedReader(new InputStreamReader(System.in));

You should not close the BufferedReader if you want to read again in the System.in.
By deduction, I assume the problem comes from that.

You have the same problem with Scanner instances.

davidxxx
  • 125,838
  • 23
  • 214
  • 215