Display records from an array

Hi

I'm using Netbeans IDE 5.0.

I'm trying to display data from an array, but it appears that that the array that i'm displaying give me null results ie:

Name = null

Account Balance = $0.00

Account ID = 0

Address = null

DOB = null

Phone Number = null

But, my ARRAY is not null, because i included a condition to check this. Why is my output still null?

I'm trying to display the records from a file. The contents in the file, i've stored in using an array in a method called readFile().

My readFile method() is as follows:

publicstaticint readFile (String file){

/*Create a customer array object*/

//Customer [] custRec;

/*Initialise counter to 0*/

//int count = 0;

/*Try-catch handler to handle exceptions, if any*/

try{

/*Initialise a file I/O object to open a text file for reading*/

File f =new File(file);

/*Initialise a Scanner object, input, to read input from a file

specified by user*/

Scanner input =new Scanner(f);

/*Check whether there is anymore input data*/

/*Count the number of customer records when scanner detects "name"*/

while (input.hasNext()){

String checkInput = input.next();

if (checkInput.equals("Name")){

count++;

}

}

custRec =new Customer[count];

/*Check if there is anymore input data*/

if (input.hasNext()){

/*Loops through EACH customer record with the 6 ATTRIBUTES: account

id, name, address, dob, phone number and balance*/

for (int i = 0; i < count; i++){

/*Read contents from textfile and stores into array*/

custRec[i] =new Customer();

/*Gets rid of "Account" and "ID" and "="*/

input.next();

input.next();

input.next();

int accountID = custRec[i].getAccountID();

accountID += input.nextInt();

/*Gets rid of "Name" and "="*/

input.next();

input.next();

String name = custRec[i].getName().toUpperCase();

name += input.nextLine();

/*Gets rid of "Address" and "="*/

input.next();

input.next();

String address = custRec[i].getAddress();

address += input.nextLine();

/*Gets rid of "DOB" and "="*/

input.next();

input.next();

String dateOfBirth = custRec[i].getDateOfBirth();

dateOfBirth += input.nextLine();

/*Gets rid of "Phone", "No" and "="*/

input.next();

input.next();

input.next();

String phoneNo = custRec[i].getPhoneNo();

phoneNo += input.next();

/*Gets rid of "Account", "Balance" and "="*/

input.next();

input.next();

input.next();

double balance = custRec[i].getBalance();

balance += input.nextDouble();

}

}

}

catch (Exception ex){

ex.printStackTrace();

}

/*Print the number of customer records*/

return count;

}

The function of the readFile is supposed to read customer records from a text file specified by user and stores them, in an array. The method reports the number of customer records sucessfully read as its return value.

The method which i'm having problem to display the records is displayRecords():

publicstaticvoid displayRecords(){

/*Loops through the array of customer records*/

for (int i = 0; i<custRec.length ; i++){

custRec[i] =new Customer();

/*Check if the array is null*/

if (custRec[i]!=null){

/*Display the customer records if array is not null*/

System.out.println("Name = "+custRec[i].getName());

System.out.printf("Account Balance = $%.2f\n", custRec[i].getBalance());

System.out.println("Account ID = "+custRec[i].getAccountID());

System.out.println("Address = "+custRec[i].getAddress());

System.out.println("DOB = "+custRec[i].getDateOfBirth());

System.out.println("Phone Number = "+custRec[i].getPhoneNo()+"\n");

}

else

/*Array is null*/

System.out.println("Array is null!");

}

}

The function of the displayRecords is supposed to print the customer records on the screen.

What is wrong here? How should i modify my code to make it work?

Help much appreciated..Thanks!!!>

[6635 byte] By [ngpamelaa] at [2007-10-3 4:09:32]
# 1

In your displayRecords method you do this for each element of the array:

1. Assign a new Customer object to it, discarding whatever was there before.

2. Check whether it is null, even though it can't possibly be null because you just assigned an object to it.

3. Print the contents of the new Customer object, which you have done nothing to assign.

So it's not surprising that you get nulls and blanks printed. You didn't initialize your Customer object in any way.

DrClapa at 2007-7-14 22:09:37 > top of Java-index,Java Essentials,Java Programming...
# 2

Ya, you're right...

Is there a way whereby i can display the customer details from a text file, customers.txt when i create a new customer object and not discarding whatever was there before?

Or must i assign the new customer object to the file?

I have to repeat what i did again in readFile() method with the input.next() methods? But the displayRecords() method is just supposed to print the customer records. Is there anyway to retrieve the customer details in the customers.txt file without going through what i did in readFile() method?

ngpamelaa at 2007-7-14 22:09:37 > top of Java-index,Java Essentials,Java Programming...