Need Help to read .txt file and store its contents

Reply

Join Date: Apr 2007
Posts: 5
Reputation: libran is an unknown quantity at this point 
Solved Threads: 0
libran libran is offline Offline
Newbie Poster

Need Help to read .txt file and store its contents

 
0
  #1
Oct 6th, 2007
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.

  1. <span class="ad_notxt"><code class="inlinecode">
  2. import java.util.*;
  3. import java.io.*;
  4. public class FranchiseManagementSystem
  5. {
  6. private BufferedReader br;
  7. private String[] str;
  8.  
  9. public FranchiseManagementSystem() throws Exception
  10. {
  11. br = new BufferedReader(new FileReader("applicants.txt"));
  12. str = new String[50];
  13. readApplicantDetails();
  14. }
  15.  
  16. private void readApplicantDetails() throws Exception
  17. {
  18. String thisLine = "";
  19. int i = 0;
  20. while ((thisLine = br.readLine()) != null)
  21. {
  22. str[i] = thisLine;
  23. i++;
  24. }
  25.  
  26. if (str.length < 5)
  27. {
  28. System.out.println("Applicant entries are less than the minimum required number.");
  29. }
  30. else
  31. {
  32. for (i=0; i<str.length; i++)
  33. System.out.println(str[i]);
  34. }
  35. }
  36.  
  37. }
  38. </code></span>




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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 192
Reputation: nschessnerd is an unknown quantity at this point 
Solved Threads: 8
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Need Help to read .txt file and store its contents

 
0
  #2
Oct 7th, 2007
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
  1. import java.util.*;
  2. import java.io.*;
  3. public class FranchiseManagementSystem
  4. {
  5. private BufferedReader br;
  6. private String[] str;
  7.  
  8. public FranchiseManagementSystem() throws Exception
  9. {
  10. br = new BufferedReader(new FileReader("applicants.txt"));
  11. str = new String[50];
  12. readApplicantDetails();
  13. }
  14.  
  15. private void readApplicantDetails() throws Exception
  16. {
  17. String thisLine = br.readLine();
  18. int i = 0;
  19. while (thisLine != null)
  20. {
  21. str[i] = thisLine;
  22. thisLine = br.readLine();
  23. i++;
  24. }
  25. if (i < 5)
  26. {
  27. System.out.println("Applicant entries are less than the minimum required number.");
  28. }
  29. else
  30. {
  31. for (int a=0; a<i; a++)
  32. System.out.println(str[a]);
  33. }
  34. }
  35.  
  36. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 771
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 104
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Need Help to read .txt file and store its contents

 
1
  #3
Oct 7th, 2007
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)
        {
            System.out.println(thisLine);
            System.out.println(i);
            str[i] = thisLine;
            System.out.println(str[i]);
            i++;
        }

        if (str.length < 5)
        {
            System.out.println("str.length < 5");
            // str.length must be at least 5 since the following println statement fails to execute
            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!
Last edited by darkagn; Oct 7th, 2007 at 4:31 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC