| | |
Having problems saving objects into an array, then accessing the data later for repo
![]() |
•
•
Join Date: Apr 2005
Posts: 27
Reputation:
Solved Threads: 0
Having problems saving objects into an array, then accessing the data later for repo
0
#1 Aug 8th, 2005
[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);
}
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);
}
Re: Having problems saving objects into an array, then accessing the data later for r
0
#2 Aug 8th, 2005
casting is what you need to do.
trying to get a apple out of an array of apples
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.
trying to get a apple out of an array of apples
Java Syntax (Toggle Plain Text)
Apple myApple = (Apple)appleArray[1];
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.
•
•
Join Date: Apr 2005
Posts: 27
Reputation:
Solved Threads: 0
Re: Having problems saving objects into an array, then accessing the data later for repo
0
#3 Aug 8th, 2005
Re: Having problems saving objects into an array, then accessing the data later for repo
0
#4 Nov 3rd, 2005
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.
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.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Window Xp IE 6 (DNS ERROR) page not found! (Viruses, Spyware and other Nasties)
- JTables (Java)
- how to add data from an array to a stack (C++)
- accessing private data members (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the Java Forum
- Previous Thread: menubar
- Next Thread: String class methods...
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game gis givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





