| | |
Need Help to read .txt file and store its contents
![]() |
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Solved Threads: 0
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.
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
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)
<span class="ad_notxt"><code class="inlinecode"> 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]); } } } </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
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
here is the fixed code. I left it as close to normal as possible
Java Syntax (Toggle Plain Text)
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.
Hopefully these println statements will help you debug your program. Don't forget to comment them all out for your final program!
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.
![]() |
Similar Threads
- Capturing data from txt file using JavaScript/HTML (JavaScript / DHTML / AJAX)
- TSP read input file problem (C++)
- read text file (C)
- Need Help in Reading characters from a text file (C++)
- how to store the data? (Java)
- Getting an array from a txt file (C)
- reading txt file into array (C++)
- How can I read from a txt file? (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
Other Threads in the Java Forum
- Previous Thread: need help for java chat application
- Next Thread: Game program
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android applet application apps array automation binary bluetooth businessintelligence button card character class client code collision component consumer crashcourse css csv database desktop eclipse ee error fractal free ftp game givemetehcodez graphics gui html image integration j2me japplet java javaarraylist javac javadoc javaee javafx javaprojects jni jpanel julia jvm linked linux loan mac method migrate mobile netbeans objects online oriented phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql swing test textfield textfields threads time tree trolltech ubuntu update utility windows





