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);

}

Edited 3 Years Ago by diafol: fixed formatting

casting is what you need to do.

trying to get a apple out of an array of apples

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.

Paul,

Thanks for your help.

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

Thanks,


Bob

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.

This article has been dead for over six months. Start a new discussion instead.