how to save data and load data from an arrayList ?

i got a run time problem .....when i try to save my data to a file and load a data from a file....i got some kind of error like that

Please enter your CD`s title : ivan

Please enter your CD`s Artist/GroupName : diw

Please enter your CD`s year of release : wid

InputMismatchException error occurred (the next token does not match the Integer regular expression, or is out of range) java.util.InputMismatchException

Invaild value....Please enter the value between 1000 & 9999

Please enter your CD`s MusicGenre(e.g.: Jazz, Blues, Funk, Classical, Rock, etc...) : w

Please enter your CD`s comment : w

Do you have another Cd ? (Y/N) : n

Saving to file

java.io.EOFException

at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)

at java.io.ObjectInputStream.readObject0(Unknown Source)

at java.io.ObjectInputStream.readObject(Unknown Source)

at Demo.loadDate(Demo.java:49)

at Demo.readDataFromConsole(Demo.java:103)

at Demo.main(Demo.java:173)

>Exit code: 0

import java.util.ArrayList;

import java.io.*;

publicclass Demo{

readOperation theRo =new readOperation();

errorCheckingOperation theEco =new errorCheckingOperation();

ArrayList<MusicCd> MusicCdList;

public Demo()

{

}

privatevoid heading()

{

System.out.println("\tTesting read data from console, save to file, reopen that file\t");

}

privatevoid saveDate()

{

MusicCdList =new ArrayList<MusicCd>( );

try

{

File f =new File("jessica.txt");

FileOutputStream fos =new FileOutputStream(f);

ObjectOutputStream oos =new ObjectOutputStream(fos);

for( MusicCd s : MusicCdList)

oos.writeObject(s);

oos.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

privatevoid loadDate()

{

MusicCdList =new ArrayList<MusicCd>( );

try

{

File g =new File("jessica.txt");

FileInputStream fis =new FileInputStream(g);

ObjectInputStream ois =new ObjectInputStream(fis);

ArrayList<String> stuff = (ArrayList<String>)ois.readObject();

for( String s : stuff ) System.out.println(s);

ois.close();

}catch (Exception ioe)

{

ioe.printStackTrace();

}

}

privatevoid readDataFromConsole()

//private void insertCd()

{

ArrayList<MusicCd> MusicCdList =new ArrayList<MusicCd>( );

readOperation theRo =new readOperation();

errorCheckingOperation theEco =new errorCheckingOperation();

MusicCd theCd;

String muiseCdsTitle;

String muiseCdsArtistOrGroupName;

int muiseCdsYearOfRelease;

int validMuiseCdsYearOfRelease;

String muiseCdsMusicGenre;

String muiseCdsAComment;

while(true)

{

String continueInsertCd ="Y";

do

{

muiseCdsTitle = theRo.readString("Please enter your CD`s title : ");

muiseCdsArtistOrGroupName = theRo.readString("Please enter your CD`s Artist/GroupName : ");

muiseCdsYearOfRelease = theRo.readInt("Please enter your CD`s year of release : ");

validMuiseCdsYearOfRelease = theEco.errorCheckingInteger(muiseCdsYearOfRelease, 1000, 9999);

muiseCdsMusicGenre = theRo.readString("Please enter your CD`s MusicGenre(e.g.: Jazz, Blues, Funk, Classical, Rock, etc...) : ");

muiseCdsAComment = theRo.readString("Please enter your CD`s comment : ");

MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsArtistOrGroupName, validMuiseCdsYearOfRelease ,

muiseCdsMusicGenre, muiseCdsAComment));

MusicCdList.trimToSize();

//saveToFile(MusicCdList);

continueInsertCd = theRo.readString("Do you have another Cd ? (Y/N) : ");

}while(continueInsertCd.equals("Y") || continueInsertCd.equals("y") );

if(continueInsertCd.equals("N") || continueInsertCd.equals("n"));

{

System.out.println("Saving to file ");

//MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));

saveDate();

loadDate();

break;

}

}

}

publicstaticvoid main(String[] args)

{

Demo one =new Demo();

one.readDataFromConsole();

}

}

how should i fix it if i want to reach the save to a file and load from a file purpose?

thx all

for much more understand of this program

import java.io.Serializable;

publicclass MusicCdimplements Serializable

{

private String musicCdsTitle;

private String artistOrGroupName;

privateint yearOfRelease;

private String musicGenre;

private String aComment;

public MusicCd()

{

musicCdsTitle ="";

artistOrGroupName ="";

yearOfRelease = 1000;

musicGenre ="";

aComment ="";

}

public MusicCd(String newMusicCdsTitle, String newArtistOrGroupName,int newYearOfRelease,

String newMusicGenre, String aNewComment)

{

musicCdsTitle = newMusicCdsTitle;

artistOrGroupName = newArtistOrGroupName;

yearOfRelease = newYearOfRelease;

musicGenre = newMusicGenre;

aComment = aNewComment;

}

public String getTitle()

{

return musicCdsTitle;

}

public String getArtistOrGroupName()

{

return artistOrGroupName;

}

publicint getYearOfRelease()

{

return yearOfRelease;

}

public String getMusicGenre()

{

return musicGenre;

}

public String getAComment()

{

return aComment;

}

publicvoid setTitle(String newMusicCdsTitle)

{

musicCdsTitle = newMusicCdsTitle;

}

publicvoid setArtistOrGroupName(String newArtistOrGroupName)

{

artistOrGroupName = newArtistOrGroupName;

}

publicvoid setYearOfRelease(int newYearOfRelease)

{

yearOfRelease = newYearOfRelease;

}

publicvoid setMusicGenre(String newMusicGenre)

{

musicGenre = newMusicGenre;

}

publicvoid setAComment(String aNewComment)

{

aComment = aNewComment;

}

/*

public boolean equalsName(MusicCd otherCd)

{

if(otherCd == null)

return false;

else

return (musicCdsTitle.equals(otherCd.musicCdsTitle));

}

*/

public String toString()

{

return("Title: " + musicCdsTitle +"\t"

+"Artist/GroupName: " + artistOrGroupName +"\t"

+"Year of release: " + yearOfRelease +"\t"

+"Music Genre: " + musicGenre +"\t"

+"Comment: " + aComment +"\t" );

}

}

[code]

import java.util.*;

publicclass readOperation{

public String readString(String userInstruction)

{

String aString =null;

try

{

Scanner scan =new Scanner(System.in);

System.out.print(userInstruction);

aString = scan.nextLine();

}

catch (NoSuchElementException e)

{

//if no line was found

System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);

}

catch (IllegalStateException e)

{

// if this scanner is closed

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return aString;

}

publicchar readTheFirstChar(String userInstruction)

{

char aChar =' ';

String strSelection =null;

try

{

//char charSelection;

Scanner scan =new Scanner(System.in);

System.out.print(userInstruction);

strSelection = scan.next();

aChar = strSelection.charAt(0);

}

catch (NoSuchElementException e)

{

//if no line was found

System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);

}

catch (IllegalStateException e)

{

// if this scanner is closed

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return aChar;

}

publicint readInt(String userInstruction){

int aInt = 0;

try{

Scanner scan =new Scanner(System.in);

System.out.print(userInstruction);

aInt = scan.nextInt();

}catch (InputMismatchException e){

System.out.println("\nInputMismatchException error occurred (the next token does not match the Integer regular expression, or is out of range) " + e);

}catch (NoSuchElementException e){

System.out.println("\nNoSuchElementException error occurred (input is exhausted)" + e);

}catch (IllegalStateException e){

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return aInt;

}

}

import java.util.Scanner;

import java.util.InputMismatchException;

import java.util.NoSuchElementException;

publicclass errorCheckingOperation

{

publicint errorCheckingInteger(int checkThing,int lowerBound,int upperBound)

{

int aInt = checkThing;

try

{

while((checkThing < lowerBound ) || (checkThing > upperBound) )

thrownew Exception("Invaild value....Please enter the value between " + lowerBound +" & " + upperBound );

}

catch (Exception e)

{

String message = e.getMessage();

System.out.println(message);

}

return aInt;

}

publicint errorCheckingSelectionValue(String userInstruction)

{

int validSelectionValue = 0;

try

{

int selectionValue;

Scanner scan =new Scanner(System.in);

System.out.print(userInstruction);

selectionValue = scan.nextInt();

validSelectionValue = errorCheckingInteger(selectionValue , 1, 5);

}

catch (NoSuchElementException e)

{

//if no line was found

System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);

}

catch (IllegalStateException e)

{

// if this scanner is closed

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return validSelectionValue;

}

}

Message was edited by:

Ivan1238

Message was edited by:

Ivan1238

[18909 byte] By [Ivan1238a] at [2007-11-27 5:41:28]
# 1

You should thoroughly check you code. It's full of problems.

For example in saveDate():

You create a new empty ArrayList and then want so save it's contents. I guess, the file size is always 0.

For example in loadDate():

Since you read from an empty file you get an EOFException. This is ok and you should catch and treat the exception properly.

You try to read the ArrayList instead of single MusicCd objects, although in saveDate you saved the single MusicCD objects. You can only read from the file what to saved in it before.

I suggest to write the size of the ArrayList as first object in the file. Then you know how much you can expect to read when loading from the file.

private void saveDate()

{

try

{

File f = new File("jessica.txt");

FileOutputStream fos = new FileOutputStream(f);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(new Integer(MusicCdList.size()));

for( MusicCd s : MusicCdList)

oos.writeObject(s);

oos.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

tobias.winterhaltera at 2007-7-12 15:18:59 > top of Java-index,Java Essentials,Java Programming...
# 2

thx for your suggestion....i just follow your instruction ...i got a run-time error again....could u give me more suggestion...i have been stuck for these problem for 2 day...pls help..

here is my run-time error after i change the code

Please enter your CD`s title : ivan

Please enter your CD`s Artist/GroupName : 2939

Please enter your CD`s year of release : 299

Invaild value....Please enter the value between 1000 & 9999

Please enter your CD`s MusicGenre(e.g.: Jazz, Blues, Funk, Classical, Rock, etc...) : 2e

Please enter your CD`s comment : ekee

Do you have another Cd ? (Y/N) : n

Saving to file

Exception in thread "main" java.lang.NullPointerException

at Demo.saveDate(Demo.java:48)

at Demo.readDataFromConsole(Demo.java:126)

at Demo.main(Demo.java:197)

Ivan1238a at 2007-7-12 15:18:59 > top of Java-index,Java Essentials,Java Programming...
# 3
i really help you guy help...pls give me a hand....what wrong with my code...how should i reach my task...save data to a file ...and reopen that data once from a file
Ivan1238a at 2007-7-12 15:18:59 > top of Java-index,Java Essentials,Java Programming...