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

So... you've got a few problems with your code... one really really bad thing is that you make each method throw an exception. that means you will never see if there are any errors. You were getting a bunch of nulls because you were storing values in an array of 50 and printing out each spot...
here is the fixed code. I left it as close to normal as possible

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 = br.readLine();
        int i = 0;
              while (thisLine != null)
        {           
               str[i] = thisLine;
              thisLine = br.readLine();
            i++;
        }
        if (i < 5)
        {
            System.out.println("Applicant entries are less than the minimum required number.");
        }
        else
        {
          for (int a=0; a<i; a++)
              System.out.println(str[a]);
                  }
    }  
    
}

Hi Libran,

To help debug your code, try adding some println statements to gain a better understanding of what is going on. Please see my comments below.

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)
        {
            [COLOR="Red"]System.out.println(thisLine);
            System.out.println(i);[/COLOR]
            str[i] = thisLine;
            [COLOR="Red"]System.out.println(str[i]);[/COLOR]
            i++;
        }

        if (str.length < 5)
        {
            [COLOR="Red"]System.out.println("str.length < 5");
            // str.length must be at least 5 since the following println statement fails to execute[/COLOR]
            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]);
        }
    }  

}

Hopefully these println statements will help you debug your program. Don't forget to comment them all out for your final program! ;)

commented: Good simple advice! +11

A flat file should contain the source IP address, destination IP address and the port numbers are defined. ( Eg.,
sourceIP="192.168.1.1" destIP="198.254.8.1" port=4000 )
All these contents have to be read by a program written in JAVA and the values should be stored in an array. there should be two separate program codes:one for flat file and another java code.How can I do this??can I get the code for this??

i want to knw like how it will read the text file application.txt?
and i'm try to run this exm in eclipse but cannot how to ?

i want to knw like how it will read the text file application.txt?
and i'm try to run this exm in eclipse but cannot how to ?

write your own code, there are examples enough on the forum to help you out.
this thread hasn't been posted in for years, why didn't you just start a thread of your own?

also: provide us with proof you've actually done something besides copying code and trying to run them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.