2017-12-27 24 views
-1

現在、顧客データベースを作成するプロジェクトで作業しています。このプロジェクトでは、ユーザが追加()、編集()、削除()、リストtextFileに保存された顧客データベースの詳細。エラー(java.io.EOFException)のためにedit()メソッドのコードを書くのに問題があります。誰かがエラーを修正して、配列のコードを変換しようとしたときにコードがよく書かれているかどうかを確認してくださいオブジェクトを配列リストに追加します。ありがとうございました。カスタマーデータベースのedit()メソッドを作成する際に問題が発生しました

メインメニューとセッターとゲッターを含めて、私が作成したクラスの数が増えているので、誰かがこの量のコードを手伝ってくれることを願っています。

ありがとうございます。

public void editCustomerDetails()throws IOException, ClassNotFoundException 
{ 
    File inFile=new File(fileName); 
    FileInputStream inFileStream=new FileInputStream(inFile); 
    ObjectInputStream inFileObjectStream = new ObjectInputStream(inFileStream); 

    List CustomerList= new ArrayList(); 

    int noOfRecords= inFileObjectStream.readInt();//error java.io.EOFException, null in java.io.DataInputStream 
    //get entire list(all records) from file 
    try 
    { 
     CustomerList =(List)inFileObjectStream.readObject(); 
    }catch(Exception e) 
    { 
     System.out.println("Error reading file-no records"); 
     noOfRecords= -1; 

    } 
    UserDetails[] customer = new UserDetails[noOfRecords]; 
    String nameToSearchFor; 
    System.out.println("Enter the name you want to search for: "); 
    nameToSearchFor= sc.next(); 
    int position =-1; 
    boolean flag=false; 
    for(int i=0; i<noOfRecords; i++) 
    { 
     CustomerList =(List)inFileObjectStream.readObject(); 

     if(CustomerList.contains(nameToSearchFor)) 
     { 
      position = i; 
      flag = true; 

     } 

    } 

    if (flag == true) 
    { 
     System.out.println("Account Number: " + customer[position].getAccountNum() + "\n" + 
           "Name: " + customer[position].getName() + "\n" + 
           "Surname: " + customer[position].getSurname() + "\n" + 
           "Email: " + customer[position].getEmail() + "\n" + 
           "Date of Birth: " + customer[position].getDateOfBirth() 
           + "\n"); 

      String n,s,email,d; 

      System.out.println("Edit name"); 
      n=sc.next(); 
      System.out.println("Edit surname"); 
      s=sc.next(); 
      System.out.println("Edit email"); 
      email=sc.next(); 
      System.out.println("Edit Date Of Birth"); 
      d=sc.next(); 

      //conversion of String to Date  
      try { 
       d=sc.nextLine(); 
       DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH); 
       Date date = formatter.parse(d); 
       customer[position].setDateOfBirth(formatter.format(date)); 

      } catch (ParseException e) { 
     //handle exception if date is not in "dd-MMM-yyyy" format 
     System.out.println(e.getMessage()); 
     } 

      customer[position].setName(n); 
      customer[position].setSurname(s); 
      customer[position].setEmail(email); 
      CustomerList.add(customer); 


      File outFile = new File(fileName); 
      FileOutputStream outFileStream = new FileOutputStream(outFile); 
      ObjectOutputStream outFileObjectStream = new ObjectOutputStream(outFileStream); 


      outFileObjectStream.writeObject(CustomerList); 
      outFileObjectStream.close(); 
    } 
    else 
    { 
     flag=false; 
     System.out.println("Customer was not found"); 
    } 
    } 

のStackTrace:あなたが最初のリード上の例外を持ったよう

java.io.EOFException 
    at java.io.DataInputStream.readInt(DataInputStream.java:392) 
    at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2823) 
    at java.io.ObjectInputStream.readInt(ObjectInputStream.java:972) 
    at Customer.editCustomerDetails(Customer.java:103) 
    at SubMenus.optionOne(SubMenus.java:29) 
    at ReservationMainMenu.main(ReservationMainMenu.java:29) 
+0

スタックトレードを追加できますか? –

+0

あなたの質問に答えた後、あなたの質問を全く別のものに変更しないでください。 – EJP

答えて

0

、はっきり入力ファイルは空である必要があります。

+0

ファイルを空にして、int noOfRecords = inFileObjectStream.readInt();を書き込もうとしたときの例外があるため、どのように修正するか、例外をキャッチする方法を知っていますか?その後も同じエラーが出ます。ありがとう – Kelsey

+0

空のファイルに何かがあることを確認して修正できます。存在しない場合は、空であることを示すこの例外が発生します。もっとあなたが望むことができるものが不明である。 – EJP