0

Hoping someone might be able to assist me, or point me in the right direction. I have a text file with a number of entries

CUST001, John Jones, 555 0505, 19/09/1981
CUST002, PeterParker, 555 1234, 0.2
CUST003, Michael Michaels, 555 4321, 19/09/1981
etc

I have an abstract superclass with constructor and accessors for the shared properties and a subclass. i then have another class, also with constructor and accessors.

I read in each line, and split it at the "," and read this into a temp array. I then create my empty array to read into it the properties from my superclass and with the contructor, i create the various objects.

problem i have run into: regular class with constructor - this work prefectly. I print out my objects after they have been created and there they are.

My subclass though, it only returns values null, null, null I assume therefore that there is an issue with my superclass and subclass.

creating objects using the concrete class constructor:

Product[] prod = new Product[20]; 
BufferedReader inFile = new BufferedReader (new FileReader("product.txt"));
String inputLine = inFile.readLine();
for (int i = 0; i < 6 ; i++)
{
    String[] tmpProd = inFile.readLine().split(","); 
    prod[i] = new Product( tmpProd[0], 
                           tmpProd[1], 
                           tmpProd[2],
                           Float.parseFloat(tmpProd[3]),
                           tmpProd[4].charAt(0));
}

"trying" to create objects from the superclass (Customer) and subClass (STCustomer):

Customer[] stdCust= new STCustomer[20];
BufferedReader inFileCust = new BufferedReader (new FileReader ("customer.txt"));
String inputCust = inFileCust.readLine();
for (int i = 0; i < 6; i++)
{
    String[] tmpCust = inFileCust.readLine().split(",");
    GregorianCalendar d = new GregorianCalendar(year, month -1, date);
    stdCust[i] = new STCustomer(    tmpCust[0],
                                 tmpCust[1], 
                                 Long.parseLong(tmpCust[2]), 
                                 d);//the block to convert date works - omitted here
}  

Is this the correct way to create the objects?

Customer[] stdCust= new STCustomer[20];
2
  • Can you rename all wording of "regular class" in your question to say either "subclass" or "superclass" I have no clue what the regular class is. IN some parts, it sounds like it refers to the superrclass, and in others, it sounds like it refers to the sublass. Commented Nov 10, 2011 at 0:38
  • Hi Georges, apologies for the confusion. i have two classes i created. one is a concrete class and the other is a abstract class with subclasses. I am trying to create objects from a text file using the subclass. doing the same with the concrete class (Product) allows me to successfully create the objects with values from the txt file. This is what I am stuck with. And it appears i am missing something obvious as i dont see anyone else with these difficulties. Commented Nov 10, 2011 at 1:03

3 Answers 3

2

The Problem is with your subclass constructor. You have to explicitly call the desired super class constructor, else the compiler will add super() as the first statement in you subclass constructor. Below is an example.

import java.util.Date;

public class Test {
         public static void main(String... abc){
             Customer[] a = new STCustomer[20];
             a[0] = new STCustomer();
             a[1] = new STCustomer("Hello","World",12L,new Date());
             a[1] = new STCustomer("Hello","World",12L);
         }
}

class Customer{
    public Customer(){
        System.out.println("Customer()");
    }

    public Customer(String a, String b, long c,Date d){
        System.out.println("Customer(String a, String b, long c,Date d)");
            // Set values to fields
    }
}

class STCustomer extends Customer{
    public STCustomer(){}

    public STCustomer(String a, String b, long c,Date d){

    }

    public STCustomer(String a, String b, long c){
      super(a,b,c,new Date());
    }
}

and output

Customer()
Customer()
Customer(String a, String b, long c,Date d)
Sign up to request clarification or add additional context in comments.

Comments

0

No, you should not use arrays. Use a List<Customer> instead and be happy about its simple API. You can use the add and size methods of it, and you don't have to keep track of the size yourself. You can also have more than 20 customers, and the code will still work.

Here is some example code:

List<Customer> customers = Lists.newArrayList();

...
while ((line = bufferedReader.readLine()) != null) {
  ...
  customers.add(new Customer(...));
}

for (Customer customer : customers) {
  System.out.println(customer.getId());
}

Comments

0

It was one of many mistakes I am making. When extending my customer class, i did not add "super(cID, cName, cPhone). This resulted in null being returned.

class STCustomer extends Customer{
//instance variables
private GregorianCalendar stCustJoinDate; 

//constructor
public STCustomer (String cID, String cName, String cPhone, 
        GregorianCalendar stCJoinDate)
{
    super(cID, cName, cPhone );
    stCustJoinDate = stCJoinDate;
}

//accessor
public GregorianCalendar getSTCJoinDate() {return stCustJoinDate;}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.