loadingData and Saving data from a list..<_>..thx all!!pls help

i am doing a muisc Cd program..but i got stuck on saving and loading data from a list after inserting my data to the list. I have a general concept on saving and loading data .And i could sucessfully saving and loading the number of item from the list. but i totally fail how to saving and loading all the object from the list ...pls help....my code is post as following ..i post all my program to be here to let u understand what i am doing..thx

====================

>javac musicCdDemo.java

>Exit code: 0

>java musicCdDemo

Please enter your CD`s title : ivan

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

Please enter your CD`s title : lll

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

Please enter your CD`s title : joho

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

Please enter your CD`s title : we are the world

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

Number of items in the list: 4

Title: we are the world

Title: joho

Title: lll

Title: ivan

Saving Data to the List......

Loading Data from the List......

Number of Music Cd in SMOA : 4<<-- i could load my num of items in list sucessfully ,,but how i could load and save all the date from or in a list ....thx

>Exit code: 0

===================================

testing driver

import java.io.*;

publicclass musicCdDemo

{

SortedList musicCdList =new SortedList();

publicvoid saveDate()

{

try

{

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

FileOutputStream fos =new FileOutputStream(f);

ObjectOutputStream oos =new ObjectOutputStream(fos);

oos.writeObject(new Integer(musicCdList.getNumberOfItems()));

//how should i implented my code to save all the object to my list

//oos.writeObject(musicCdList.getMusicCd()); //<- i know i should do something on here...but i don`t know i need to do exactly to save all my musicCdList`s object..

oos.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

publicvoid loadDate()

{

try

{

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

FileInputStream fis =new FileInputStream(g);

ObjectInputStream ois =new ObjectInputStream(fis);

//first : list size (Integer)

Integer i = (Integer)ois.readObject();// <<- i could load my numberOfitem from the list...

System.out.println("Number of Music Cd in SMOA : " + i);

//but how do i load all my object from the list..i don`t know what i should do here..

//pls help...

ois.close();

}catch (Exception ioe)

{

ioe.printStackTrace();

}

}

publicvoid insertCd()

{

String musicCdsTitle;

while (true)

{

String continueInsertCd ="Y";

ReadOperation theRo =new ReadOperation();

do

{

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

if(!musicCdList.isDuplicate(musicCdsTitle))

{

musicCdList.insert(new MusicCd(musicCdsTitle));//, muiseCdsArtistOrGroupName, validMuiseCdsYearOfRelease, muiseCdsMusicGenre, muiseCdsAComment));

}

else

{

System.out.println("\nYou got a duplicate Cd`s Title " +"'" + musicCdsTitle +"'"

+"\nwith one of the existed CD in SMOA."

+"\nHence, This cd can not be inserted to SMOA!!");

}

// saveToFile(MusicCdList);

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

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

System.out.println("\nNumber of items in the list: "

+ musicCdList.getNumberOfItems() +"\n" + musicCdList.toString() );

break;

}

}

publicstaticvoid main(String[] args)

{

musicCdDemo testing =new musicCdDemo();

testing.insertCd();

System.out.println("Saving Data to the List......");

testing.saveDate();

System.out.println("Loading Data from the List......\n");

testing.loadDate();

}

}

=====================================================

musieNode for my sortedList

publicclass MusicCdNode

{

private MusicCd cd;

private MusicCdNode next;

// Default constructor

public MusicCdNode()

// Description: Initialize the reference for the cd object and the value of next to null.

// Postcondition: cd = null; next = null;

{

cd =null;

next =null;

}

// Parameterized constructor

public MusicCdNode(MusicCd aCd)

// Description: Set the reference for the cd object according to the parameters and value of next to null.

// Postcondition: cd = aCd; next = null;

{

cd = aCd;

next =null;

}

public MusicCd getMusicCd()

{

returnnew MusicCd(cd.getCdTitle());

}

public MusicCdNode getNext()

{

return next;

}

publicvoid setMusicCd(MusicCd aCd)

{

cd =new MusicCd(aCd.getCdTitle());

}

publicvoid setNext(MusicCdNode aCd)

{

next = aCd;

}

}

========================================================

music cd object

// File: MusicCd.java

// Author: Chi Lun To (Ivan To)

// Created on: June 5, 2007

// Class Description

// The MusicCd class defines a music cd object that contain the CD`s title, CD`s artist/GroupName,

// CD`s yearOfRelease , Cd`s music genre, and any comment of the Cd`s. This class provides functions

// to access the musicCdsTitle, artistOrGroupName, yearOfRelease, musicGenre, and aComment variable.

// Class Invariant: All MusicCd objects have a string musicCdsTitle, string artistOrGroupName, integer yearOfRelease

// String musicGenre, and String aComment. A string type musicCdsTitle,artistOrGroupName, musicGenre,or aComment of "None"

// indicates no real name specified yet. A integer yearOfRelease of 1000 indicates no real years specific yet.

publicclass MusicCd

{

String theCdTitle;// the CD`s Title

// Default constructor

public MusicCd()

// Description: Initialize theCdTitle to empty string

// Postcondition: theCdTitle = " ";

{

theCdTitle =" ";

}//end constructor

// Parameterized constructor

public MusicCd(String aCdTitle)

// Description: Set theCdTitle according to the parameters

// Postcondition: theCdTitle = aCdTitle;

{

theCdTitle = aCdTitle;

}// end constructor

// Accessor function : getCdTitle( ) function

public String getCdTitle()

// Description: Method to return the theCdTitle

// Postcondition: the value of theCdTitle is returned

{

return theCdTitle;

}// end getCdTitle( ) function

// Mutator function: setCdTitle( ) function

publicvoid setCdTitle(String aCdTitle)

// Description: Method to set theCdTitle according to the parameter

// Postcondition: theCdTitle = aCdTitle;

{

theCdTitle = aCdTitle;

}// end setCdTitle( ) function

// toString( ) function

public String toString()

// Description: Method to return the theCdTitle

// Postcondition: the value of theCdTitle is returned as String

{

return("Title: " + theCdTitle );

}// end toString( ) function

}

=======================================================

for better understanding..thc

publicabstractclass ShellSortedList

{

protected MusicCdNode head;

protectedint numberOfItems;

public ShellSortedList()

{

head =null;

numberOfItems = 0;

}

publicint getNumberOfItems()

{

return numberOfItems;

}

public MusicCdNode getMusicCdNode()

{

return head;

}

publicboolean isEmpty()

{

return( numberOfItems == 0 );

}

publicboolean isDuplicate(String newCD)

{

boolean found =false;

MusicCdNode current = head;

for(int i=0; i < numberOfItems; i++)

{

if(current.getMusicCd().getCdTitle().equalsIgnoreCase(newCD))

{

System.out.println("Duplicate Cd is found !!");

found =true;

}

current = current.getNext();

}

return found;

}

public String toString()

{

String listString =" ";

MusicCdNode current = head;

for(int i=0; i < numberOfItems; i++)

{

listString += current.getMusicCd().toString() +"\n";

current = current.getNext();

}

return listString;

}

}

====================================================

inferface sortedList

publicinterface SortedListInterface<T>

{

//public SortedList();

publicvoid insert( T anElement );

}

=======================================================

publicclass SortedListextends ShellSortedListimplements SortedListInterface<MusicCd>

{

public SortedList()

{

super();

}

publicvoid insert( MusicCd aCd )

{

MusicCdNode cdNode =new MusicCdNode( aCd );

cdNode.setNext( head );

head = cdNode;

numberOfItems++;

}

}

=====================================================

reading operatio

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;

}

}

thx all....

Message was edited by:

Ivan1238

Message was edited by:

Ivan1238

[21230 byte] By [Ivan1238a] at [2007-11-27 7:05:30]
# 1

How much data to you plan on saving? Is this just for learning where you may save a couple CD's or is this for something more large-scale where you may have 100 or more CD's stored? If the latter, you might want to consider using a database. There are several SQL adaptations available as open source that you could choose from to integrate with your Java code.

If the former, perhaps you could save it as XML. I haven't used XML with Java (I've used it w/ C# where an xml library comes with the language), and I think you have to obtain an XML library (could be wrong), but again, I think that there are some good ones available as open source.

Message was edited by:

petes1234

petes1234a at 2007-7-12 18:56:43 > top of Java-index,Java Essentials,Java Programming...
# 2

thx for your reply ..let me make more specfic on my problem..it is unlimitied how data i save and load..as long as they is my object in my list, i just want to save it all to my file ...and then i want to load it from a file...but i stuck on save the object `s name ...here is music`s title...i only could save all the items in my list ..pls help....<_> ...thx

Ivan1238a at 2007-7-12 18:56:43 > top of Java-index,Java Essentials,Java Programming...
# 3
thx for your suggestion....
Ivan1238a at 2007-7-12 18:56:43 > top of Java-index,Java Essentials,Java Programming...
# 4

Serialization comes with the language. Just define how to read and write each object, and you serialize them to or from a file. It's a little more complicated than that, but it might be the quickest solution you've got:

http://java.sun.com/j2se/1.5.0/docs/guide/serialization/index.html

http://java.sun.com/docs/books/tutorial/javabeans/persistence/index.html

kevjavaa at 2007-7-12 18:56:43 > top of Java-index,Java Essentials,Java Programming...
# 5

maybe i try to make more clear of i want to do ..so u guy could help me more directly...thx ..i want to output to be as following :

====================

>javac musicCdDemo.java

>Exit code: 0

>java musicCdDemo

Please enter your CD`s title : ivan

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

Please enter your CD`s title : lll

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

Please enter your CD`s title : joho

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

Please enter your CD`s title : we are the world

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

Number of items in the list: 4

Title: we are the world

Title: joho

Title: lll

Title: ivan

Saving Data to the List......

Loading Data from the List......

Number of Music Cd in SMOA : 4 <<-- i could load my num of items in list sucessfully ,,but how i could load and save all the date from or in a list ....thx

Title: we are the world <<-but i don`t know how to do this part ...loading and saving

Title: joho

Title: lll

Title: ivan

>Exit code: 0

Ivan1238a at 2007-7-12 18:56:43 > top of Java-index,Java Essentials,Java Programming...
# 6
thx for your sugeestion .....do u have the other way to do it....cos i almost reach there by using my way...it could save the num of item i have to a file and load it from a file,,,,i just stuck on how to save all the object ,music cd title, to a file and load it from a file...thx
Ivan1238a at 2007-7-12 18:56:43 > top of Java-index,Java Essentials,Java Programming...
# 7
could anyone tell me what did i miss in my code ....i think i almost be there la....but i am not sure what i did wrong ...i could save and load the num of item in my list ..but i do`nt know how to save and load the music cd`s title that i just do the insertion....pls help
Ivan1238a at 2007-7-12 18:56:43 > top of Java-index,Java Essentials,Java Programming...