2016-09-28 11 views
0

最初にjuetという名前のフォルダを作成し、そのフォルダ内に2つのパッケージを作成しました。一つ目は、大学では全学生の世話をする学生パッケージですJavaのDataInputStreamからの入力を読み取る際にエラーが発生しました

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); 
    } 
} 

私が作ったもう一つのパッケージが最後で、その後大学に

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); 
    } 
} 

をすべてのスタッフの世話をし、スタッフであります私は私が舞にUniversityオブジェクトを呼び出していたときに問題が生じているパッケージ

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(); 
      } 
     } 
    } 
} 

の両方を使用されたMyUniversityクラスを行っていますn個のクラスには、二つの入力

1)学生 ので、私は3 を入力して、再び、それはスタッフ

のための容量を入力してください) 2を求める。しかし、私はそこに整数を入力したときにそれがあるため.Enter能力を求めていますエラー

java.io.Exceptionを無限回 を実行していると示す:ストリーム java.io.BufferedInputStream.getBufIfopen(BufferedIで

を閉じましたnputStream.java:170)java.io.DataInputStream.readLineで

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

(DataInputStream.java:513)

は私を助けてください事前に感謝します

答えて

-1

あなたは間違ったクラスを使用しています。 DataInputStreamおよび方法readUTF()およびreadInt()は、コンソールからテキストを読み取るために使用できません。 DataOutputStreamを使用して、異なるJavaプログラムでエンコードされたバイナリコンテンツを読み込むように設計されています。

次の質問と回答は、あなたが右のそれを行う方法を示しています How can I read input from the console using the Scanner class in Java?

+0

System.inはコンソールではありません。 – MGorgon

+0

@Wojciech Kazior:省略されたメソッド 'readLine()'を使用しているため – Robert

+0

@Mgorgon:System.inはコンソールではありませんが、OPがコンソールとしてコンソールを使用していることが明らかです。 – Robert

関連する問題