944,100 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 18798
  • Java RSS
Oct 6th, 2007
0

Need Help to read .txt file and store its contents

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  1. <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>




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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
libran is offline Offline
5 posts
since Apr 2007
Oct 7th, 2007
0

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

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
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006
Oct 7th, 2007
1

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

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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Mar 23rd, 2010
0
Re: Need Help to read .txt file and store its contents
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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muktha krish is offline Offline
1 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: 2d char array - Vigenere cipher
Next Thread in Java Forum Timeline: Help! Loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC