LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2004, 05:22 PM   #1
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Rep: Reputation: 35
java student with serialize type question


ok, im working on an assignment for class, the teacher could not answer my question so i will ask here.

the situation:
there are three files: WriteCountryInfo.java CountryInfo.java and ReadCountrynfo.java
the write class makes a series of countryinfo objects via a constructor in the countryinfo class then serializes them and creates a file. the readcountryinfo class de-serializes the data from the created file back into countryinfo objects.
in the book these programs use an array of countryinfo objects and writes them (and reads them) one at a time in a for loop.
the idea is to put the countryinfo objects in an ArrayList so that it can write all the objects to the file in one step instead of using the loop.

i have the write portion down, like this:

//prog assignment 8.9
import java.io.*;
import java.util.ArrayList;

public class WriteCountryInfo
{
public static void main(String[] args) throws IOException
{
FileOutputStream file = new FileOutputStream("countries.dat");
ObjectOutputStream outStream= new ObjectOutputStream(file);

ArrayList countries = new ArrayList();

countries.add(new CountryInfo("United States of America","USA","Washington, D.C.", 9629091L, 278058900L));
countries.add(new CountryInfo("Russia","RUS","Moscow",17075200L,145470200L));
countries.add(new CountryInfo("Italy","ITA","Rome",301230L,57679800L));
countries.add(new CountryInfo("Sweden","SWE","Stockholm",449964L,8875100L));
countries.add(new CountryInfo("Poland","POL","Warsaw",312685L,38633900L));
int index=0;
while(index<countries.size())
{




System.out.println(countries.get(index));
index++;
}

outStream.writeObject(countries);
}
}

...

like so. from here i can reference the individual indexes of the ArrayList no problem.
but when it reads the file it is currently pulling it up as one whole chunk without seperating back into CountryInfo objects.

//prog 8.9
import java.io.*;
import java.util.ArrayList;

public class ReadCountryInfo
{
public static void main(String[] args) throws Exception
{
FileInputStream file = new FileInputStream("countries.dat");
ObjectInputStream inStream=new ObjectInputStream(file);

ArrayList countries=new ArrayList();
countries.add(inStream.readObject());
int index=0;
while(index<countries.size())
{

System.out.println(countries.get(index));
index++;
}
}
}

i know i must need a reference to the CountryInfo constructor somewhere but i think my text book is a little unclear.
sincere thanks in advance.
 
Old 03-29-2004, 05:29 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
I think you need to cast the object back into a CountryInfo type.
Code:
...
ArrayList countries=new ArrayList();
countries.add((CountryInfo)inStream.readObject());
int index=0;
...
 
Old 03-29-2004, 05:53 PM   #3
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
I'm a long time non-OOPer, but I'm starting to learn a little these days. I'm just wondering (and this may be a bit of an aside, and certainly not helpful) why you'd want three separate classes like that? Shouldn't there just be a CountryInfo class which has operations for reading or writing its data, serially?

I just haven't seen classes named like operations (in my admittedly narrow OOP experience)...

Last edited by rkef; 03-29-2004 at 05:57 PM.
 
Old 03-29-2004, 05:55 PM   #4
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Original Poster
Rep: Reputation: 35
thats what i was thinking but... it compiles but when i run it i get:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList
at ReadCountryInfo.main(ReadCountryInfo.java:13)
???
 
Old 03-29-2004, 05:57 PM   #5
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Original Poster
Rep: Reputation: 35
Quote:
Originally posted by rkef
I'm a long time non-OOPer, but I'm starting to learn a little these days. I'm just wondering (and this may be a bit of an aside, and certainly not helpful) why you'd want three separate classes like that? Shouldn't there just be a CountryInfo class which has methods for reading or writing its data, serially?
i think that its just for ease of representation in the text book. show 1 prog to write and one to read and see how seperate classes can interact or something like that?
 
Old 03-29-2004, 06:01 PM   #6
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Try creating a new CountryInfo object and assigning the casted object back to it:
Code:
ArrayList countries=new ArrayList();
CountryInfo temp = (CountryInfo)inStream.readObject();
countries.add(temp);
int index=0;
or perhaps you need separate files for each of the serialised objects? I'm not sure how serialised objects work, though I do have quite a bit of Java experience if you do get stuck.
 
Old 03-29-2004, 06:02 PM   #7
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
Quote:
i think that its just for ease of representation in the text book. show 1 prog to write and one to read and see how seperate classes can interact or something like that?
Ah... fair enough; thanks for taking the time to answer my question . g/l.
 
Old 03-29-2004, 06:19 PM   #8
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Original Poster
Rep: Reputation: 35
ok i tried that and it didnt like the'(' coming right after the 'new' so i tried CountryInfo temp = new CountryInfo(inStream.readObject()); but it doesnt like that either.

i appreciate your help and if you come up with anything else then cool. im going to keep trying some stuff as well and if i get it ill post answer.
 
Old 03-29-2004, 06:20 PM   #9
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Original Poster
Rep: Reputation: 35
this is the part i didnt post, the constructor class:

//prog 8.9
//
import java.io.Serializable;

public class CountryInfo implements Serializable
{
private String name, abbreviation, capitol;
private long area, population;
public CountryInfo()
{}


public CountryInfo (String cName, String cAbbreviation, String cCapitol, long cArea, long cPopulation)
{
name=cName;
abbreviation=cAbbreviation;
capitol=cCapitol;
area=cArea;
population=cPopulation;
}

public String toString()
{
String result ="Name: "+name+"\n";
result +="Abbreviation:"+abbreviation+"\n";
result +="Capitol: "+capitol+"\n";
result +="Area: "+area+"\n";
result +="Population: "+population+"\n";
result +="^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n";

return result;
}
}

~
 
Old 03-29-2004, 07:07 PM   #10
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Quote:
Originally posted by Brain Drop
ok i tried that and it didnt like the'(' coming right after the 'new' so i tried CountryInfo temp = new CountryInfo(inStream.readObject()); but it doesnt like that either.

i appreciate your help and if you come up with anything else then cool. im going to keep trying some stuff as well and if i get it ill post answer.
Tried what? You're gonna have to quote which ever of us you're replying to.
 
Old 03-29-2004, 07:22 PM   #11
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Original Poster
Rep: Reputation: 35
sorry, i was replying to you and your last suggestion:

code:
ArrayList countries=new ArrayList();
CountryInfo temp = (CountryInfo)inStream.readObject();
countries.add(temp);
int index=0;

so far your first suggestion of :
countries.add((CountryInfo)inStream.readObject());

seems to come the closest but i havent figured out yet how to avoid the cast exception. I'm going back to reading my text book for awhile, maybe the answer is hiding.
 
Old 03-29-2004, 09:36 PM   #12
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Original Poster
Rep: Reputation: 35
i got it! easier than i thought.

all i had to do was:

//prog 8.9
import java.io.*;
import java.util.ArrayList;

public class ReadCountryInfo
{
public static void main(String[] args) throws Exception
{
FileInputStream file = new FileInputStream("countries.dat");
ObjectInputStream inStream=new ObjectInputStream(file);

ArrayList countries=(ArrayList)inStream.readObject();


int index=0;
while(index<countries.size())
{

System.out.println(countries.get(index));
index++;
}
}
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't type in Java applet using Firefox? JamieBrown Linux - Software 12 07-26-2010 02:52 AM
wxObject + serialize bianchi Programming 0 12-01-2005 08:47 AM
should i have any type of server set up to use java mail?? gajaykrishnan Programming 1 01-28-2005 04:39 AM
test type in java exodist Programming 8 02-18-2004 10:25 PM
what is the best way for student to enter java code Brain Drop Programming 11 01-01-2004 03:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration