0

Firstly I have made a folder named juet and then I have made two packages inside that folder. First one is Student package which takes care of all the students in university

package juet.stud;

import java.io.IOException;
import java.io.DataInputStream;

public class Student {

    String name;
    public int roll_no;
    int std;
    char grade;

    public Student() {
        try (DataInputStream in = new DataInputStream(System.in)) {
            System.out.println("Enter name of student:");
            name = in.readUTF();
            System.out.println("Enter roll no.:");
            roll_no = in.readInt();
            System.out.println("Enter std:");
            std = in.readInt();
            System.out.println("Enter grade");
            grade = in.readChar();
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public void showInfo() {
        System.out.println("Name of student: " + name);
        System.out.println("Roll no.: " + roll_no);
        System.out.println("Std: " + std);
        System.out.println("Grade: " + grade);
    }
}

Another package which I have made is Staff which takes care of all staff in the university

package juet.staff;

import java.io.IOException;
import java.io.DataInputStream;

public class Staff {

    public int id;
    String name, specialization;
    char group;

    public Staff() {
        try (DataInputStream in = new DataInputStream(System.in)) {
            System.out.println("Enter id:");
            id = in.readInt();
            System.out.println("Enter name:");
            name = in.readUTF();
            System.out.println("Enter area of specialization:");
            specialization = in.readUTF();
            System.out.println("Enter group");
            group = in.readChar();
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public void showInfo() {
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Area of specialization: " + specialization);
        System.out.println("Group: " + group);
    }
}

And then at the last I have made MyUniversity Class in which I was using both the packages

package juet;

import juet.stud.Student;
import juet.staff.Staff;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.Console;

class University {

    Student[] stu;
    Staff stf[];
    int studCount, staffCount;

    University() {
        try (DataInputStream in = new DataInputStream(System.in)) {
            System.out.println("Enter capacity for students:");
            int x = Integer.parseInt(in.readLine());
            //stu = new Student[x];
            System.out.println("Enter capacity for staff:");
            x = Integer.parseInt(in.readLine());
            stf = new Staff[x];
            studCount = staffCount = 0;
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    void newStudent() {
        stu[studCount] = new Student();
        studCount++;
    }

    void studInfo(int roll
    ) {
        int i;
        for (i = 0; i < studCount; i++) {
            if (stu[i].roll_no == roll) {
                stu[i].showInfo();
                return;
            }
        }
        System.out.println("No match found.");
    }

    void newStaff() {
        stf[staffCount] = new Staff();
        staffCount++;
    }

    void staffInfo(int id
    ) {
        int i;
        for (i = 0; i < staffCount; i++) {
            if (stf[i].id == id) {
                stf[i].showInfo();
                return;
            }
        }
        System.out.println("No match found.");
    }
}

class MyUniversity {

    public static void main(String args[]) throws IOException {
        University juet = new University();
        int ch;
        DataInputStream in = new DataInputStream(System.in);
        while (true) {
            System.out.println("\tMAIN MENU\n");
            System.out.println("1. Add student\n2. Add staff member\n3. Display info about specific student\n4. Display info about specific staff member\n0. Exit\n\tEnter your choice");

            try {
                ch = Integer.parseInt(in.readLine());
                switch (ch) {
                    case 1:
                        juet.newStudent();
                        break;
                    case 2:
                        juet.newStaff();
                        break;
                    case 3:
                        System.out.println("Enter roll no. of student to display info:");
                        int roll = in.readInt();
                        juet.studInfo(roll);
                        break;
                    case 4:
                        System.out.println("Enter ID of staff member to display info:");
                        int id = in.readInt();
                        juet.staffInfo(id);
                        break;
                    case 0:
                        return;
                    default:
                        System.out.println("Incorrect choice.");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Problem is arising when I am calling University object in the main class it is asking for two inputs

1).Enter capacity for students so I enter 3 and again it ask for 2)Enter capacity for staff

But when I enter the integer there it is running infinite times and showing error

java.io.Exception:Stream closed

at java.io.BufferedInputStream.getBufIfopen(BufferedInputStream.java:170)

atjuet.MyUniversity.main(MyUniversity.java:76)

at java.io.DataInputStream.readLine(DataInputStream.java:513)

Please help me Thanks in advance

ecbrodie
  • 9,397
  • 19
  • 62
  • 109
Vikas Kumar
  • 133
  • 9

1 Answers1

-1

You are using the wrong class. DataInputStream and the methods readUTF() and readInt() can not be used for reading text from the console. It is designed to read binary content encoded by a different Java program using DataOutputStream.

The following question and answers show you how to do it right: How can I read input from the console using the Scanner class in Java?

Community
  • 1
  • 1
Robert
  • 33,260
  • 14
  • 84
  • 130
  • System.in isn't the Console. – MGorgon Sep 28 '16 at 18:46
  • @Wojciech Kazior: Because you are using the deprected method `readLine()` – Robert Sep 29 '16 at 07:27
  • @Mgorgon: System.in is not the console but the question clearly indicates that the OP uses it as console. – Robert Sep 29 '16 at 07:27
  • @Wojciech Kazior You don't have to close Scanner. If you don't close it System.in will not be closed. – Robert Sep 29 '16 at 09:07
  • @ Wojciech Kazior: `close()` is not called automatically. Scanner does not have an `finalize` destructor at all: See [Scanner.java](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Scanner.java) – Robert Sep 29 '16 at 11:38
  • Thanks @ Wojciech Kazior: Your answer was correct thanks a lot – Vikas Kumar Sep 29 '16 at 21:14