14 Replies - 576 Views - Last Post: 11 May 2012 - 05:41 AM Rate Topic: -----

#1 xxshankar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 31-January 10

Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 03:35 AM

I am trying to add "Book" to a file but I would like to ensure that all book have a unique ISBN number. I keep getting the IOException Error (Line 83).The comparison might might be the problem but I not sure

import java.util.*;
import java.io.*;

import jls.*;

public class ItemFile {

    //output Data object itemFile
    private ObjectOutputStream itemOutput;
    private ObjectInputStream itemInput;
    Scanner input = new Scanner(System.in);
    Item itemRecord = new Item();
    ArrayList itemArray = new ArrayList();

    //Add New Items to Library 
    public void addNewItem() {
        String itemISBN, itemTitle, itemPublisher, itemAuthor, itemType = " ";
        int itemValue$, itemCount;
        //enter item ISBN unique number
        System.out.print("Enter Book ISBN Number: ");
        itemISBN = input.nextLine();

        //CHECK IF ISBN EXIST
        //Open item DeserializeFile
        openDeserializeItemFile();
        try {
            itemArray = (ArrayList) itemInput.readObject();
            for (int i = 0; i < itemArray.size(); i++) {
                itemRecord = (Item) itemArray.get(i);
                if (itemRecord.getItemISBN() == itemISBN) {
                    System.out.println("Invalid ISBN Numbet.. ItemISBN Exist!!");
                }//end if
                else {
                    //Selsct what item to be added.
                    System.out.print("Please Select Library Item Type to be Added\n\t"
                            + "1. Book\n\t"
                            + "2. Periodicals\n\t"
                            + "3. DVDs\\CDs");
                    int type = input.nextInt();
                    if (type == 1) {
                        itemType = "Book";
                    } else if (type == 2) {
                        itemType = "Periodicals";
                    } else if (type == 3) {
                        itemType = "DVDs\\CDs";
                    } else if (type < 1 && type > 3) {
                        System.out.println("Invali Selection");
                    }

                    //enter item title
                    System.out.print("Enter Book Title: ");
                    itemTitle = input.nextLine();

                    //enter publisher
                    System.out.print("Enter Book Publisher: ");
                    itemPublisher = input.nextLine();

                    //enter author
                    System.out.print("Enter Book Author: ");
                    itemAuthor = input.nextLine();

                    //enter item value
                    System.out.print("Enter Book Value: $");
                    itemValue$ = input.nextInt();

                    //Number of item
                    System.out.print("Enter they Number of Item");
                    itemCount = input.nextInt();

                    itemRecord = new Item(itemISBN, itemType, itemTitle, itemPublisher, itemAuthor, itemValue$, itemCount);
                    //add all items to array list
                    itemArray.add(itemRecord);

                    //write array list to file
                    itemOutput.writeObject(itemArray);
                }//end else
            }//end for
        }//end try
        catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to Create Object");
        }//end catch ClassNotFoundException
        catch (IOException ioException) {
            System.err.println("ERROR Reading File");
        }//end catch IO

        //CLOSE DESERIALIZE
        closeDeserializeItemFile();

    }//End method AddNewItem

    /*
     * =====SERIALZE========
     */
    //create-open file
    public void openSerializeItemFile() {
        try {
            itemOutput = new ObjectOutputStream(new FileOutputStream("item.jls"));
        }//end try
        catch (IOException ioExecption) {
            System.err.println("System Read/Write Error");
        }//end catch
    }//END Method OpenItemFile

    //Serializing File
    public void writeSerializeItemsFile() {
        try {
            //write array list to file
            itemOutput.writeObject(itemArray);
        }//end try
        //catch error if user enter invalid info
        catch (NoSuchElementException elementException) {
            System.err.println("Invalid Input");
        }//end catch
        //catch error if user dont have access to open or write to file
        catch (IOException ioException) {
            System.err.println("System Read/Write Error");
        }//end catch
    }

    //Close Serialze File
    public void closeSerializeItemFile() {
        try {
            if (itemOutput != null) {
                itemOutput.close();
            }
        }//end try
        catch (IOException ioException) {
            System.err.println("Close Item File Error");
            System.exit(1);
        }//end catch
    }//end closeFile

    /*
     * =====DESERIALZE========
     */
    //Open Item Deserialize FilFile 
    public void openDeserializeItemFile() {
        try {
            itemInput = new ObjectInputStream(new FileInputStream("item.jls"));
        }//end try
        catch (IOException ioExecption) {
            System.err.println("System Error Opening File");
        }//end catch
    }//END Method openItemDeserializeFile

    //Deserialze ItemISBN ONLY
    public void readDeserializeItemFile() {
        try {
            itemArray = (ArrayList) itemInput.readObject();
            for (int i = 0; i < itemArray.size(); i++) {
                itemRecord = (Item) itemArray.get(i);
            }//end for
        }//end try
        catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to Create Object");
        }//end catch ClassNotFoundException
        catch (IOException ioException) {
            System.err.println("ERROR Reading File");
        }//end catch IO
    }//End Method deSerialseItem

    //Close Deserialze File
    public void closeDeserializeItemFile() {
        try {
            if (itemInput != null) {
                itemInput.close();
            }
        }//end try
        catch (IOException ioException) {
            System.err.println("Close Item File Error");
            System.exit(1);
        }//end catch
    }//end closeFile

    //Rent Item
    public void itemRentBook() {
        System.out.println("Enter Book/Item ISBN : ");
        String itemISBN = input.nextLine();

        try {

            itemArray = (ArrayList) itemInput.readObject();
            for (int i = 0; i < itemArray.size(); i++) {
                itemRecord = (Item) itemArray.get(i);
                //Find ID
                if (itemRecord.getItemISBN() == itemISBN) {
                    //Add 1 to userItemCount
                    itemArray.add(itemRecord.getItemCount() - 1);

                    break;
                }//end IF       
                else {
                    System.out.println("Invalid Item");
                }//end else
            }//end for
        }//end try
        catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to Create Object");
        }//end catch ClassNotFoundException
        catch (IOException ioException) {
            System.err.println("ERROR Reading File");
        }//end catch IO

    }//end user Rent book

    //Reutrnt Item
    public void itemReturnBook() {
        System.out.println("Enter Book/Item ISBN : ");
        String itemISBN = input.nextLine();

        try {
            itemArray = (ArrayList) itemInput.readObject();
            for (int i = 0; i < itemArray.size(); i++) {
                itemRecord = (Item) itemArray.get(i);
                //Find ID
                if (itemRecord.getItemISBN() == itemISBN) {
                    //Add 1 to userItemCount
                    itemArray.add(itemRecord.getItemCount() + 1);
                    break;
                }//end IF       
                else {
                    System.out.println("Invalid Item");
                }//end else
            }//end for
        }//end try
        catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to Create Object");
        }//end catch ClassNotFoundException
        catch (IOException ioException) {
            System.err.println("ERROR Reading File");
        }//end catch IO

    }//end user Rent book
}//END ItemFile CLASS




Is This A Good Question/Topic? 0
  • +

Replies To: Comparing and Updating values in SerializeFiles in Java

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 03:48 AM

http://technojeeves....free/118-errors

Quote

if (itemRecord.getItemISBN() == itemISBN) {


should also be

if (itemRecord.getItemISBN().equals(itemISBN)) {


(and similar for all other String comparisons)

This post has been edited by g00se: 09 May 2012 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 xxshankar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 31-January 10

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 04:11 AM

I will only use == if its a int or double?


View Postg00se, on 09 May 2012 - 03:48 AM, said:

http://technojeeves....free/118-errors

Quote

if (itemRecord.getItemISBN() == itemISBN) {


should also be

if (itemRecord.getItemISBN().equals(itemISBN)) {


(and similar for all other String comparisons)

Was This Post Helpful? 0
  • +
  • -

#4 xxshankar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 31-January 10

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 04:19 AM

I am getting the same error in this class

import java.util.*;
import java.io.*;

import jls.*;

public class LibraryFile {

    //Scanner input
    Scanner input = new Scanner(System.in);
    //Data object libFile
    private ObjectOutputStream libOutput;
    private ObjectInputStream libInput;
    //Array List
    ArrayList libraryArray = new ArrayList();
    //Library Class
    Library libraryRecord = new Library();

    //Add New Library
    public void addNewLibrary() {
        int libraryID;
        String libraryName, libraryParish, libraryType = " ";

        System.out.println("Enter Library ID (1000-9999): ");
        libraryID = input.nextInt();
        if (libraryID < 999 || libraryID > 9999) {
            System.out.println("Library ID Should be between 1000 - 9999 ");
        }//end if
        else {

            //Open Deserialize File
            openDeserializeLibraryFile();
            //Check if Library ID Exist
            try {
                libraryArray = (ArrayList) libInput.readObject();
                for (int i = 0; i < libraryArray.size(); i++) {
                    libraryRecord = (Library) libraryArray.get(i);
                    if (libraryRecord.getLibraryID() == libraryID) {
                        System.out.println("Invalid Library ID..");
                    }//end IF
                    else {
                        //Library Name
                        System.out.println("Enter Library Name: ");
                        libraryName = input.nextLine();

                        //Library Address(Parish)
                        System.out.println("Enter Library Parish: ");
                        libraryParish = input.nextLine();

                        //Select Library Type
                        System.out.println("Please Select Type\n\t"
                                + "1. School Library\n\t"
                                + "2. Parish Library\n\t");
                        int type = input.nextInt();
                        switch (type) {
                            case 1:
                                libraryType = "School Library";
                                break;
                            case 2:
                                libraryType = "Parish Library";
                                break;
                            default:
                                System.out.print("Invalid Selection");
                                break;
                        }//end switch

                        libraryRecord = new Library(libraryID, libraryName, libraryParish, libraryType);
                        //add library to libraryArray
                        libraryArray.add(libraryRecord);
                        libOutput.writeObject(libraryArray);
                        
                        
                    }//end else
                }//end for
            }//end try
            catch (ClassNotFoundException classNotFoundException) {
                System.err.println("Unable to Create Object");
            }//end catch ClassNotFoundException
            catch (IOException ioException) {
                System.err.println("ERROR Reading File");
            }//end catch IO

            //Close Deserialize File
            closeDeserializeFile();
        }//end else
    }//end method AddNewLibrary

    /*
     * ======SERIALIZE FILE=====
     */
    //create-open file
    public void openSerializeLibraryFile() {
        try {
            libOutput = new ObjectOutputStream(new FileOutputStream("library.jls"));
        }//end try
        catch (IOException ioExecption) {
            System.err.println("System Read/Write Error");
        }//end catch
    }//END Method OpenLibraryFile

    //Serialze Library
    public void writeSerializeLibraryFile() {
        try {
            libOutput.writeObject(libraryArray);
        }//end try
        //catch error if user enter invalid info
        catch (NoSuchElementException elementException) {
            System.err.println("Invalid Input");
        }//end catch
        //catch error if user dont have access to open or write to file
        catch (IOException ioException) {
            System.err.println("System Read/Write Error");
        }//end catch
    }//End Serialze Library

    //Close Serialize Library
    public void closeSerializeLibraryFile() {
        try {
            if (libOutput != null) {
                libOutput.close();
            }
        }//end try
        catch (IOException ioException) {
            System.err.println("Close Item File Error");
            System.exit(1);
        }//end catch
    }//end closeFile

    /*
     * ====DESERIALIZE=====
     */
    //Open Library DeserializeFile File 
    public void openDeserializeLibraryFile() {
        try {
            libInput = new ObjectInputStream(new FileInputStream("library.jls"));
        }//end try
        catch (IOException ioExecption) {
            System.err.println("System Error Opening File");
        }//end catch
    }//END Method openItemDeserializeFile

    //Deserialze Library
    public void readDeserialieLibrary() {
        try {
            libraryArray = (ArrayList) libInput.readObject();
            for (int i = 0; i < libraryArray.size(); i++) {
                libraryRecord = (Library) libraryArray.get(i);
            }//end for
        }//end try
        catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to Create Object");
        }//end catch ClassNotFoundException
        catch (IOException ioException) {
            System.err.println("ERROR Reading File");
        }//end catch IO
    }//End deSerialzeLibrary

    //Close DeserialIze File
    public void closeDeserializeFile() {
        try {
            if (libInput != null) {
                libInput.close();
            }
        }//end try
        catch (IOException ioException) {
            System.err.println("Close Item File Error");
            System.exit(1);
        }//end catch
    }//end closeFile
    
    
    public void libraryRentBook() {
        System.out.println("Enter Library ID: ");
        int libraryID = input.nextInt();

        try {
            libraryArray = (ArrayList) libInput.readObject();
            for (int i = 0; i < libraryArray.size(); i++) {
                libraryRecord = (Library) libraryArray.get(i);
                //Find ID
                if(libraryRecord.getLibraryID() == libraryID){                   
                    break;
                }//end IF       
                else{
                    System.out.println("Invalid ID");
                }//end else
            }//end for
        }//end try
        catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to Create Object");
        }//end catch ClassNotFoundException
        catch (IOException ioException) {
            System.err.println("ERROR Reading File");
        }//end catch IO

    }//


Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 05:00 AM

Quote

I will only use == if its a int or double?


Only if the type is a Java primitive, yes, or if you want to compare reference equality

Quote

I am getting the same error in this class

http://technojeeves....free/118-errors
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 08:00 AM

instead of

System.err.println("ERROR Reading File");

use

System.err.println("ERROR Reading File: " + ioException);

at least you will know what is the error instead of just "ERROR"
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 10:09 AM

Quote

System.err.println("ERROR Reading File: " + ioException);


won't really be a whole lot more use. You need the stack trace

System.err.println("ERROR Reading File:");
ioException.printStackTrace();

Was This Post Helpful? 0
  • +
  • -

#8 xxshankar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 31-January 10

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 06:29 PM

This is the error:

ERROR Reading File
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at jls.LibraryFile.addNewLibrary(LibraryFile.java:43)
at jls.JLS.main(JLS.java:45)


View Postg00se, on 09 May 2012 - 10:09 AM, said:

Quote

System.err.println("ERROR Reading File: " + ioException);


won't really be a whole lot more use. You need the stack trace

System.err.println("ERROR Reading File:");
ioException.printStackTrace();

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 09 May 2012 - 07:05 PM

java.io.EOFException
at jls.LibraryFile.addNewLibrary(LibraryFile.java:43)


EndOfFile you try to read passed the end of the file at line 43
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 10 May 2012 - 03:51 AM

Quote

 libraryArray = (ArrayList) libInput.readObject();


Is the above line 43 in your actual source code?
Was This Post Helpful? 0
  • +
  • -

#11 xxshankar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 31-January 10

Re: Comparing and Updating values in SerializeFiles in Java

Posted 10 May 2012 - 05:50 AM

Yes that is line 43 in my code

View Postg00se, on 10 May 2012 - 03:51 AM, said:

Quote

 libraryArray = (ArrayList) libInput.readObject();


Is the above line 43 in your actual source code?

Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 10 May 2012 - 05:58 AM

The general comment made by pbl is correct. You now need to find out why that's happening. Good candidates are probably a repeated attempted read or a read of an empty file
Was This Post Helpful? 0
  • +
  • -

#13 tekbwainz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-May 12

Re: Comparing and Updating values in SerializeFiles in Java

Posted 11 May 2012 - 04:32 AM

As long as i know if you serialize an object and if you add a new property you can't cahnge the serialized file. The only way is to get all serialized object to deserialize them and to serialize them with the new property in different ser files.
One of the best solutions for serialization and modifying without breakin the deployment :) Proto Buffer
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Comparing and Updating values in SerializeFiles in Java

Posted 11 May 2012 - 05:07 AM

Quote

if you add a new property


But that's not happening here is it?
Was This Post Helpful? 0
  • +
  • -

#15 tekbwainz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-May 12

Re: Comparing and Updating values in SerializeFiles in Java

Posted 11 May 2012 - 05:41 AM

package com.clouway.inout.task6;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: tekbwainz
 * Date: 5/4/12
 * Time: 4:27 PM
 * To change this template use File | Settings | File Templates.
 */

/**
 *  Data class is a class which serialize objects. It used two methods. One of them saves the object and the other one gets the serialized object.
 */
public class DataClass {
  /**
   *
   * @param out   - is the output serialize file
   * @param o     - the object which is going to be serialized
   * @throws IOException
   */
  public void saveObject(FileOutputStream out, Object o) throws IOException {
    ObjectOutputStream  outputFile = new ObjectOutputStream(out);
    outputFile.writeObject(o);
    out.close();
  }

  /**
   *
   * @param in    - the file containing the serialized object
   * @return    - the deserialized object
   * @throws IOException
   * @throws ClassNotFoundException
   */
  public Object getObject(FileInputStream in) throws IOException, ClassNotFoundException {
    ObjectInputStream intro = new ObjectInputStream(in);
    return intro.readObject();
  }

  public void compareObjects(Employee object1, Employee object2 , FileOutputStream out) throws IOException {
    if(object1.getEmail().equals(object2.getEmail())){
        System.out.println("The two object has the same emails");
    }
      else
        saveObject(out,object2);
  }
}

That is the class

package com.clouway.inout.task6;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: tekbwainz
 * Date: 5/7/12
 * Time: 10:16 AM
 * To change this template use File | Settings | File Templates.
 */
public class Main {
  public static void main(String[] args) {
    try {
      FileOutputStream firstFile = new FileOutputStream("/home/tekbwainz/workspaces/idea/myproject/src/com/clouway/inout/seriliazed.ser");
        FileOutputStream secondFile= new FileOutputStream("/home/tekbwainz/workspaces/idea/myproject/src/com/clouway/inout/seriliazed2.ser");
      Employee george = new Employee();
      george.setName("George");
      george.setAge(23);
      george.setEmail("georgievvt@gmail.com");
      DataClass serialObject = new DataClass();
      serialObject.saveObject(firstFile, george);
      Employee georgeDeserialized = (Employee) serialObject.getObject(new FileInputStream("/home/tekbwainz/workspaces/idea/myproject/src/com/clouway/inout/seriliazed.ser"));
      Employee stefan = new Employee();
        stefan.setName("Stefan");
        stefan.setAge(19);
        stefan.setEmail("georgiev@gmail.com");
        serialObject.compareObjects(george,stefan,secondFile);
        Employee stefanDeserialized = (Employee) serialObject.getObject(new FileInputStream("/home/tekbwainz/workspaces/idea/myproject/src/com/clouway/inout/seriliazed2.ser"));
     georgeDeserialized.printInfo();
        stefanDeserialized.printInfo();
    } catch (IOException e) {
      System.out.println("You have entered invalid path! ");
    } catch (ClassNotFoundException e) {
      System.out.println("Invalid path of the seriliazed file! ");
    }


  }
}




So i have 1 file where i serialize the first object after that i create a second object deserialize the first one and compare , if they are with the same email the new file is not created with the new object if they are with different emails it works.

Problem
You have to put cerate a method to put all files in one place and with a loop to check them all


P.S i hope i helped you a little i can do much more when i get back home because now i am at work and i have other things to do
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1