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!!!>

