954,224 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help to read .txt file and store its contents

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.

<code>import java.util.*;
import java.io.*;
public class FranchiseManagementSystem
{
    private BufferedReader br;
    private String[] str;
    
    public FranchiseManagementSystem() throws Exception
    {
        br = new BufferedReader(new FileReader(&quot;applicants.txt&quot;));
        str = new String[50];
        readApplicantDetails();
    }
    
    private void readApplicantDetails() throws Exception
    {
        String thisLine = &quot;&quot;;
        int i = 0;
        while ((thisLine = br.readLine()) != null)
        {
            str[i] = thisLine;
            i++;
        }
        
        if (str.length &lt; 5)
        {
            System.out.println(&quot;Applicant entries are less than the minimum required number.&quot;);
        }
        else
        {
            for (i=0; i&lt;str.length; i++)
                System.out.println(str[i]);
        }
    }  
    
}</code>



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

libran
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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]);
                  }
    }  
    
}
nschessnerd
Posting Whiz in Training
216 posts since Dec 2006
Reputation Points: 10
Solved Threads: 8
 

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.

<code>import java.util.*;
import java.io.*;
public class FranchiseManagementSystem
{
    private BufferedReader br;
    private String[] str;
    
    public FranchiseManagementSystem() throws Exception
    {
        br = new BufferedReader(new FileReader(&quot;applicants.txt&quot;));
        str = new String[50];
        readApplicantDetails();
    }
    
    private void readApplicantDetails() throws Exception
    {
        String thisLine = &quot;&quot;;
        int i = 0;
        while ((thisLine = br.readLine()) != null)
        {
            System.out.println(thisLine);
            System.out.println(i);
            str[i] = thisLine;
            System.out.println(str[i]);
            i++;
        }

        if (str.length &lt; 5)
        {
            System.out.println(&quot;str.length &lt; 5&quot;);
            // str.length must be at least 5 since the following println statement fails to execute
            System.out.println(&quot;Applicant entries are less than the minimum required number.&quot;);
        }
        else
        {
            for (i=0; i&lt;str.length; i++)
                System.out.println(str[i]);
        }
    }  
    
}</code>


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

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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

muktha krish
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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 ?

sambhu singh
Newbie Poster
1 post since Mar 2012
Reputation Points: 10
Solved Threads: 0
 
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.

stultuske
Posting Sensei
3,122 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Post: Markdown Syntax: Formatting Help
You