Frasher Gray
Frasher Gray

Reputation: 618

BufferedReader Closes at the Beginning of a For-Loop in Java

I am using BufferedReader to take in multiple inputs inside of a for a loop; however, when the first input comes in during the first time through, it throws the error

java.io.Exception: Stream closed

Despite the fact that I don't close it until after the for loop ends. The code is below:

        BufferedReader menu = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; players.length > i; i++) {
            System.out.println("Choose the " + i + " player");
            System.out.println("1. New Player");
            System.out.println("2. Old Player");
            int choice2 = Integer.parseInt(menu.readLine());
            System.out.println("Which team are you on?");
            int playersTeam = Integer.parseInt(menu.readLine());
            Person player = new Person();
            if (choice2 == 1) {
                player.squadFilling();
                player.playerNaming();
            } else if (choice2 == 2) {
                break;
            }
        }
        menu.close();

What is causing the crash, and how can I fix it? Thanks for your time!

Stacktrace:

java.io.IOException: Stream closed
    at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:168)
    at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:297)
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
    at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
    at java.base/java.io.InputStreamReader.read(InputStreamReader.java:181)
    at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
    at Main.main(Main.java:409)

Upvotes: 0

Views: 123

Answers (3)

Frasher Gray
Frasher Gray

Reputation: 618

I missed some .close() commands placed elsewhere in my code.

Upvotes: 0

Joshua Peddle
Joshua Peddle

Reputation: 31

I think you should try moving menu.close(); to a different method called at the end. As well move the BufferedReader menu to a higher visibility out of the method and just use menu = new BufferedReader(new InputStreamReader(System.in)); in method.

BufferedReader menu;
void method(){
    menu = new BufferedReader(new InputStreamReader(System.in));
    for (int i = 0; players.length > i; i++) {
        System.out.println("Choose the " + i + " player");
        System.out.println("1. New Player");
        System.out.println("2. Old Player");
        int choice2 = Integer.parseInt(menu.readLine());
        System.out.println("Which team are you on?");
        int playersTeam = Integer.parseInt(menu.readLine());
        Person player = new Person();
        if (choice2 == 1) {
            player.squadFilling();
            player.playerNaming();
        } else if (choice2 == 2) {
            break;
        }
    }
    cleanup();
}

This allows us to create a method cleanup().

private void cleanup(){
    menu.close();
}

The reason I think this might help is I believe menu.close() is being called while the writer is still finishing up from it's last loop. I think adding cleanup() as a separate method will work.

Upvotes: 1

Sagar Joon
Sagar Joon

Reputation: 1417

In Java 8, its not throwing any Exception(trying same code). Its working fine.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

 public static void main(String[] args) throws NumberFormatException, IOException {

    BufferedReader menu = new BufferedReader(new InputStreamReader(System.in));
    for (int i = 0; 2 > i; i++) {
                    System.out.println("Choose the " + i + " player");
                    System.out.println("1. New Player");
                    System.out.println("2. Old Player");
                    int choice2 = Integer.parseInt(menu.readLine());
                    System.out.println("Which team are you on?");
                    int playersTeam = Integer.parseInt(menu.readLine());
                    if (choice2 == 1) {
                        continue;
                    } else if (choice2 == 2) {
                        break;
                    }
                    
    }
    menu.close();
 }
}

Upvotes: 0

Related Questions