944,184 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3391
  • Java RSS
Aug 8th, 2005
0

Having problems saving objects into an array, then accessing the data later for repo

Expand Post »
[FONT=Courier New]Hi,

I having problems with loading created objects into an array, then being able to access the records later in the program.

The following is a listing of my current program. The code I have thus far, reads information from a file, builds the objects (DVD, Ipod, or Walkman, then loads the objects into an array. My code appears like it's working except that I can't look at what's in the objects in the arrays.

I'm new to Java, so any help you can give me will be greatly appreciated.

Thanks,


My code in question:

/********************************************************************
* Name: Inventory
* Purpose:
*
* 1.Create an abstract inventory class with 3 children classes called:
*
* a. DVDplayer
* b. IPod
* c. Walkman
*
* 2. In an input file, create some objects of each class- the object
* name should be the serial number of the object.
*
* 3. Create 3 instance variables of each child class with info that
* describes the object (Example: color, #of songs it holds, price).
*
* 4. Your program should read the input file and the output file should
* consist of a tally of:
*
* a. each type of object (DVD player, IPod and Walkman)
* b. the serial number of each of those objects
* c. the value of each type of stock
*
* Date: 08/06/2005
* Author: Bob Richardson
* References: None
*********************************************************************/
import java.text.*;
import cs1.Keyboard;
import java.text.NumberFormat;
import java.io.*;

abstract class MusicInv
{
String Description = " ";
int SerialNum = 0;

public MusicInv()
{
Description = " ";
SerialNum = 0;
}
}

class DVD extends MusicInv
{
String Color = " ";
double Price = 0.00;

public DVD()
{
Color = " ";
Price = 0.00;
}

public DVD(String DescriptionIn, int SerialNumIn, String ColorIn, double PriceIn)
{
this.Description = DescriptionIn;
this.SerialNum = SerialNumIn;
this.Color = ColorIn;
this.Price = PriceIn;
}
}

class Ipod extends MusicInv
{
String Color = " ";
double Price = 0.00;
int MaxSongs = 0;

public Ipod(String DescriptionIn, int SerialNumIn,
String ColorIn, double PriceIn, int MaxSongsIn)
{
this.Description = DescriptionIn;
this.SerialNum = SerialNumIn;
this.Color = ColorIn;
this.Price = PriceIn;
this.MaxSongs = MaxSongsIn;
}
}


class Walkman extends MusicInv
{
String Color = " ";
double Price = 0.00;

public Walkman(String DescriptionIn, int
SerialNumIn, String ColorIn, double PriceIn)
{
this.Description = DescriptionIn;
this.SerialNum = SerialNumIn;
this.Color = ColorIn;
this.Price = PriceIn;
}
}

class Inventory
{
public static void main(String args[]) throws IOException
{
char c;
final int MAX = 50;
String line = " ",
ColorIn = " ",
DescriptionIn = " ",
SerialNumStr = " ",
PriceStr = " ",
MaxSongsStr = " ";

int SerialNumIn = 0,
MaxSongsIn = 0,
i = 0;

double PriceIn = 0.00;

FileReader fr = new FileReader("Inventory.txt");
BufferedReader br = new BufferedReader(fr);

PrintWriter out = new PrintWriter(
new BufferedWriter(

new FileWriter("Invout.txt")));
String recout;
DVD[] DVDS = new DVD[MAX];
Ipod[] Ipods = new Ipod[MAX];
Walkman[] Walkmans = new Walkman[MAX];

while((line=br.readLine()) != null0)
{
i++;
String[] columns = line.split(" ");
DescriptionIn = columns[0];
SerialNumStr = columns[1];
ColorIn = columns[2];
PriceStr = columns[3];
MaxSongsStr = columns[4];

if (columns[0].equals("DVDPlayer"))
{
SerialNumIn = Integer.parseInt(SerialNumStr);
PriceIn = Double.parseDouble(PriceStr);
DVD DVDPlayer = new DVD(DescriptionIn, SerialNumIn,
ColorIn, PriceIn);
DVDS[i] = DVDPlayer;
System.out.println(DescriptionIn+" "+SerialNumIn+" "+ColorIn+" "+PriceIn+" "+DVDPlayer);

}
else
if (columns[0].equals("IPod"))
{
SerialNumIn = Integer.parseInt(columns[1]);
PriceIn = Double.parseDouble(columns[3]);
MaxSongsIn = Integer.parseInt(columns[4]);
Ipod IpodPlayer = new Ipod(DescriptionIn, SerialNumIn, ColorIn,
PriceIn, MaxSongsIn);
}
else
if (columns[0].equals("Walkman"))
{
SerialNumIn = Integer.parseInt(columns[1]);
PriceIn = Double.parseDouble(columns[3]);
Walkman WalkmanPlayer = new Walkman(DescriptionIn,
SerialNumIn, ColorIn, PriceIn);
}
}
for (DVD Description : DVDS)
{
[ System.out.println(Description);

}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
Aug 8th, 2005
0

Re: Having problems saving objects into an array, then accessing the data later for r

casting is what you need to do.

trying to get a apple out of an array of apples
Java Syntax (Toggle Plain Text)
  1. Apple myApple = (Apple)appleArray[1];
when you put something into an array it is treated as an Object (as in the class object) as opposed to as the class Apple to look at it like it was an apple again you would have to cast it.

Hence the "(Apple)" I hope this helps..

also try to use the code tags (it make it much easyer to read), cheers I hope this helps.


NVM casting dosn't seem to matter with an array odly enuf


but im pritty certain that class member varibles will default to private and therefor be out of scope for reading outside of the class.
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Aug 8th, 2005
0

Re: Having problems saving objects into an array, then accessing the data later for repo

Paul,

Thanks for your help.

And, where can I see an example of Code Tags?

Thanks,


Bob
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
Nov 3rd, 2005
0

Re: Having problems saving objects into an array, then accessing the data later for repo

Casting with Strings can be tricky. Use "toString" if you can.
I came across this one recently which interseted me...

i is an int, value is an Object and intf is an Integer Number Format
and value happened be be assigned the integer 9 in the application

try {i = intf.parse(value.toString()).intValue();}
catch(ParseException e){i = 0;}

was fine but...

try {i = intf.parse((String)value).intValue();}
catch(ParseException e){i = 0;}

compiled ok but caused a run-time casting error with the same value used above

I thought the (String) cast used the toString method but it clearly doesn't

If anyone can offer me a wise explanation of this behaviour it might help me understand what goes on.
Reputation Points: 10
Solved Threads: 1
Light Poster
Gargol is offline Offline
46 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: menubar
Next Thread in Java Forum Timeline: Compiling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC