The code on which I am working, suppose to read the contents of a file "applicants.txt" linewise and store it in a string array. Each element of array should hold one line and so on. I also want to automate the code so that when end of file is reached it should stop reading file.
Find below my code and the structure of "applicants.txt" file. Current code displays only last two lines from str array and a series of 'null' strings thereafter. I am using BlueJ environment to develop and run this code.
Kindly guide me in correct direction.
import java.util.*;
import java.io.*;
public class FranchiseManagementSystem
{
private BufferedReader br;
private String[] str;
public FranchiseManagementSystem() throws Exception
{
br = new BufferedReader(new FileReader("applicants.txt"));
str = new String[50];
readApplicantDetails();
}
private void readApplicantDetails() throws Exception
{
String thisLine = "";
int i = 0;
while ((thisLine = br.readLine()) != null)
{
str[i] = thisLine;
i++;
}
if (str.length < 5)
{
System.out.println("Applicant entries are less than the minimum required number.");
}
else
{
for (i=0; i<str.length; i++)
System.out.println(str[i]);
}
}
}
Structure of "applicants.txt" file
Cynthia Powell 99031911 F 17 18000 Y
Peter Best 99051234 M 32 70000 N
Andrew White 99042367 M 25 20000 Y
Jane Asher 99075235 F 29 5000 Y
Malcom Evans 98763476 M 18 15000 Y