i have this code:
//enrolling a Student.
String S,W,F;
String file,file1,file2;
String S1="
"+jTextField1.getText().trim();
String S2="
"+jTextField2.getText().trim();
String L=jTextField1.getText().trim()+" "+jTextField2.getText().trim()+"
";
if (S1.equals("
") && S2.equals("
"))
{
JOptionPane.showMessageDialog(this,"Enter CRN and your ID");
return;
}
else if (S2.equals("
"))
{
JOptionPane.showMessageDialog(this,"Insert CRN!");
return;
}
else if (S1.equals("
"))
{
JOptionPane.showMessageDialog(this,"Insert you ID!");
return;
}
file="enrollments.txt";
file1="students.txt";
file2="courses.txt";
try{
BufferedReader in = new BufferedReader ( new FileReader(file));
BufferedReader in1 = new BufferedReader ( new FileReader(file1));
BufferedReader in2 = new BufferedReader ( new FileReader(file2));
while((W=in1.readLine())!=null)
{
String[] fields= W.split(" ");
if ((fields[0])!=jTextField1.getText().trim())
{
JOptionPane.showMessageDialog(this,"Enter a valid ID number.");
return;
}
}
while((F=in2.readLine())!=null)
{
String[] fields1= F.split(" ");
if ((fields1[0])!=jTextField2.getText().trim())
{
JOptionPane.showMessageDialog(this,"Enter a valid CRN");
return;
}
}
while ((S=in.readLine())!=null)
{
if (S.equals(L))
{
JOptionPane.showMessageDialog(this,"This Student Already Registered This Course: Cannot Add twice!");
return;
}
}
in.close();
BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
out.append(L);
out.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
}
i want to check two files then i would procceed checking the third
the output is always : Insert a valid ID!!
what to do
????
10xxx
Comments
:
: //enrolling a Student.
: String S,W,F;
: String file,file1,file2;
:
: String S1="
"+jTextField1.getText().trim();
: String S2="
"+jTextField2.getText().trim();
:
: String L=jTextField1.getText().trim()+" "+jTextField2.getText().trim()+"
";
:
: if (S1.equals("
") && S2.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Enter CRN and your ID");
: return;
: }
:
: else if (S2.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Insert CRN!");
: return;
: }
: else if (S1.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Insert you ID!");
: return;
: }
:
: file="enrollments.txt";
: file1="students.txt";
: file2="courses.txt";
:
: try{
: BufferedReader in = new BufferedReader ( new FileReader(file));
: BufferedReader in1 = new BufferedReader ( new FileReader(file1));
: BufferedReader in2 = new BufferedReader ( new FileReader(file2));
:
: while((W=in1.readLine())!=null)
: {
: String[] fields= W.split(" ");
: if ((fields[0])!=jTextField1.getText().trim())
: {
: JOptionPane.showMessageDialog(this,"Enter a valid ID number.");
: return;
: }
:
: }
:
:
: while((F=in2.readLine())!=null)
: {
: String[] fields1= F.split(" ");
: if ((fields1[0])!=jTextField2.getText().trim())
: {
: JOptionPane.showMessageDialog(this,"Enter a valid CRN");
: return;
: }
:
: }
:
:
:
: while ((S=in.readLine())!=null)
: {
: if (S.equals(L))
: {
: JOptionPane.showMessageDialog(this,"This Student Already Registered This Course: Cannot Add twice!");
: return;
: }
: }
:
: in.close();
: BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
: out.append(L);
: out.close();
:
: }
:
: catch (IOException e) {
: JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
: }
:
: i want to check two files then i would procceed checking the third
:
: the output is always : Insert a valid ID!!
:
: what to do
:
: ????
: 10xxx
:
The problem is as follows: Suppose you have a file with the following IDs:
[code]
0
1
2
3
4
[/code]
If the user wants to check record with ID 2, the program opens the file, reads the first line starting the while-loop. It splits the first line, and finds a 0 for fields[0]. Checking this against the user input (2), finds it to be unequal and reports an invalid ID, and stops the loop.
If the user had requested ID 0, then the loop starts the first record matches the users input, so no message is returned. The loop continues and record with ID 1 is read. Obviously this doesn't match the user input and an invalid ID message is given.
The solution to this is to change your coding to the following:
[code]
while((W=in1.readLine())!=null)
{
String[] fields= W.split(" ");
if ((fields[0])[red]==[/red]jTextField1.getText().trim())
{
// Found the requested ID, continue processing record
processStudentData(fields);
return;
}
}
// No ID matched, thus invalid ID was given
JOptionPane.showMessageDialog(this,"Enter a valid ID number.");
[/code]
processStudentData() would then do something with the fields, such as display the record, calculate the GPA, allow the user to change the record, etc.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//enrolling a Student.
String S,W,F;
String file,file1,file2;
String S1="
"+jTextField1.getText().trim();
String S2="
"+jTextField2.getText().trim();
String L=jTextField1.getText().trim()+","+jTextField2.getText().trim();
if (S1.equals("
") && S2.equals("
"))
{
JOptionPane.showMessageDialog(this,"Enter CRN and your ID");
return;
}
else if (S2.equals("
"))
{
JOptionPane.showMessageDialog(this,"Insert CRN!");
return;
}
else if (S1.equals("
"))
{
JOptionPane.showMessageDialog(this,"Insert you ID!");
return;
}
file="enrollments.txt";
file1="students.txt";
file2="courses.txt";
try{
BufferedReader in1 = new BufferedReader ( new FileReader(file1));
while((W=in1.readLine())!=null)
{
String[] fields= W.split(",");
if ((fields[0])==jTextField1.getText().trim())
{
BufferedReader in2 = new BufferedReader ( new FileReader(file2));
while((F=in2.readLine())!=null)
{
String[] fields1= F.split(",");
if ((fields1[0])==jTextField2.getText().trim())
{
BufferedReader in = new BufferedReader ( new FileReader(file));
while ((S=in.readLine())!=null)
{
if ((S).equals(L))
{
JOptionPane.showMessageDialog(this,"This Student Already Registered This Course: Cannot Add twice!");
return;
}
}
in.close();
BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
out.append(L+"
");
out.close();
}
}
JOptionPane.showMessageDialog(this,"Course Doesnot Exist!");
return;
}
}
JOptionPane.showMessageDialog(this,"Student is not Registered in the Student List!");
return;
}
catch (IOException e) {
JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
}
}
STILL IT IS ALWAYS GIVING ME STUDENT NOT REGISTERED IN THE STUDENT LIST ALTHOUGHT THE ID NUMBER ACTUALLY IS IN THE students.txt
am afraid that the error is because of the terminators (like
or or )
the files are such that the fields are seperated by commas
example of student.txt:
2001225,Pamella,CCE
2001235,Anderson,ME
so for the courses.txt !!!
plzzzzzzzzz help thankssss
:
: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
: //enrolling a Student.
: String S,W,F;
: String file,file1,file2;
:
: String S1="
"+jTextField1.getText().trim();
: String S2="
"+jTextField2.getText().trim();
:
: String L=jTextField1.getText().trim()+","+jTextField2.getText().trim();
:
: if (S1.equals("
") && S2.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Enter CRN and your ID");
: return;
: }
:
: else if (S2.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Insert CRN!");
: return;
: }
: else if (S1.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Insert you ID!");
: return;
: }
:
: file="enrollments.txt";
: file1="students.txt";
: file2="courses.txt";
:
: try{
:
: BufferedReader in1 = new BufferedReader ( new FileReader(file1));
: while((W=in1.readLine())!=null)
: {
: String[] fields= W.split(",");
: if ((fields[0])==jTextField1.getText().trim())
: {
: BufferedReader in2 = new BufferedReader ( new FileReader(file2));
: while((F=in2.readLine())!=null)
: {
: String[] fields1= F.split(",");
: if ((fields1[0])==jTextField2.getText().trim())
: {
: BufferedReader in = new BufferedReader ( new FileReader(file));
: while ((S=in.readLine())!=null)
: {
: if ((S).equals(L))
: {
: JOptionPane.showMessageDialog(this,"This Student Already Registered This Course: Cannot Add twice!");
: return;
: }
: }
:
: in.close();
: BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
: out.append(L+"
");
: out.close();
: }
: }
: JOptionPane.showMessageDialog(this,"Course Doesnot Exist!");
: return;
: }
: }
: JOptionPane.showMessageDialog(this,"Student is not Registered in the Student List!");
: return;
:
: }
: catch (IOException e) {
: JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
: }
: }
:
:
: STILL IT IS ALWAYS GIVING ME STUDENT NOT REGISTERED IN THE STUDENT LIST ALTHOUGHT THE ID NUMBER ACTUALLY IS IN THE students.txt
:
: am afraid that the error is because of the terminators (like
or or )
:
: the files are such that the fields are seperated by commas
:
: example of student.txt:
:
: 2001225,Pamella,CCE
: 2001235,Anderson,ME
:
: so for the courses.txt !!!
:
:
: plzzzzzzzzz help thankssss
:
I cannot make heads or tails out of your code. You open a lot of files and perform a lot of
loops.
Try using more than 1 method to make your code more readable. Also check out the style codes,
especially the one for codes.
Also you could try to add some dialog boxes to check what the exact values of the fields are.
To aid debugging try something like this:
[code]
: BufferedReader in1 = new BufferedReader ( new FileReader(file1));
: while((W=in1.readLine())!=null)
: {
: String[] fields= W.split(",");
// System.out.print(W);
: if ((fields[0])==jTextField1.getText().trim())
: {
System.out.print("student found: "+W);
}
}
System.out.print("student not found");
[/code]
This way you are sure that the search code is correct, You can also detect errors in the
algorithm.
I apologize for taking a lot of your time
1.I want to Delete a record by entering a key e.g enter student ID...search for it in the file of extension .txt, and delete is (erase string ) from file.
I dont know the code and i found nothing on the net that might give me a help!!!
2. want to similarly search for the record given a key (i know how to search but dont know the rest) want to modify another field in this record be replacing a substring
Please may u hellp me with those issues especially the first. and regarding (1.) i need to remove the empty line by shifting all other records upward
thanks
I really appreciate ur help
:
: 1.I want to Delete a record by entering a key e.g enter student ID...search for it in the file of extension .txt, and delete is (erase string ) from file.
:
: I dont know the code and i found nothing on the net that might give me a help!!!
:
: 2. want to similarly search for the record given a key (i know how to search but dont know the rest) want to modify another field in this record be replacing a substring
:
: Please may u hellp me with those issues especially the first. and regarding (1.) i need to remove the empty line by shifting all other records upward
:
: thanks
:
: I really appreciate ur help
:
Both must be done by copying the file, and changing the record while copying. Chnaging a student record in pseudocode:
[code]
in = source file
out = destination file
while not endOfFile(in) {
in.readStudent()
if (student.ID == searchID) {
Change student
}
out.writeStudent();
}
FileDelete(in)
FileRename(out to in)
[/code]
Deleting a record:
[code]
in = source file
out = destination file
while not endOfFile(in) {
in.readStudent()
if (student.ID != searchID) {
out.writeStudent();
}
}
FileDelete(in)
FileRename(out to in)
[/code]
The last two statements can be done using a File object, the rest should be easy enough to code.
my pblm is as follows:
i have a text file called "courses.txt"
how to call this file within the program????
i was trying to transform the string f= "courses.txt" into a file name!!
this is my code
plzzzzzzzzzzzz
help
String S,W;
String S2="",W2="";
String file ="courses.txt";
File file1 = new File("coursesDelete.txt");//create a file to copy needed records and omit un-needed ones
String S1=jTextField5.getText().trim();
if (S1.equals("
"))
{
JOptionPane.showMessageDialog(this,"Enter CRN to Delete Course!");
return;
}
try{
BufferedReader in = new BufferedReader ( new FileReader(file));
BufferedWriter out = new BufferedWriter ( new FileWriter(file1,true));
while ((S=in.readLine())!=null)
{
String [] L = S.split(",");
if(L[0]!=(S1)) //copy all courses to the new file except the one that you want to delete
{
out.append(S+"
");
}
}
out.close();
in.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(this,"Error Opening File: Check File Availability!");
}
/***************cannot do!!*************************
boolean success = file.delete();
if (!success) {
// Deletion failed
JOptionPane.showMessageDialog(this,"Error Deleting File!");
return;
}
boolean success1 = file1.renameTo("courses.txt");
if (!success1) {
// File was not successfully renamed
JOptionPane.showMessageDialog(this,"Error Renaming File!");
return;
}
*/
:
: my pblm is as follows:
:
: i have a text file called "courses.txt"
:
: how to call this file within the program????
:
: i was trying to transform the string f= "courses.txt" into a file name!!
:
: this is my code
:
: plzzzzzzzzzzzz
: help
:
: String S,W;
: String S2="",W2="";
:
: String file ="courses.txt";
: File file1 = new File("coursesDelete.txt");//create a file to copy needed records and omit un-needed ones
:
: String S1=jTextField5.getText().trim();
:
: if (S1.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Enter CRN to Delete Course!");
: return;
: }
:
: try{
:
: BufferedReader in = new BufferedReader ( new FileReader(file));
: BufferedWriter out = new BufferedWriter ( new FileWriter(file1,true));
:
: while ((S=in.readLine())!=null)
: {
: String [] L = S.split(",");
: if(L[0]!=(S1)) //copy all courses to the new file except the one that you want to delete
: {
: out.append(S+"
");
: }
: }
:
: out.close();
: in.close();
:
: }
: catch (IOException e) {
: JOptionPane.showMessageDialog(this,"Error Opening File: Check File Availability!");
: }
:
: /***************cannot do!!*************************
: boolean success = file.delete();
: if (!success) {
: // Deletion failed
: JOptionPane.showMessageDialog(this,"Error Deleting File!");
: return;
: }
:
: boolean success1 = file1.renameTo("courses.txt");
: if (!success1) {
: // File was not successfully renamed
: JOptionPane.showMessageDialog(this,"Error Renaming File!");
: return;
: }
: */
:
First create a file object. Then you can use delete()/renameTo(). As you can see here both functions are not static: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
[code]
File temp = new File("courses.txt");
temp.delete();
File temp = new File("courses2.txt");
temp.renameTo("courses.txt");
[/code]
thankssssssssss
plz the last thing...with the method below pgram runs but nothing is done:
ArrayList myList1;
myList1= new ArrayList(100);
String S;
String file;
String S1= jTextField5.getText().trim();
File temp = new File("tempCourses.txt");
file="courses.txt";
if (S1.equals("
"))
{
JOptionPane.showMessageDialog(this,"Enter Your ID Number!");
return;
}
try{
BufferedReader in = new BufferedReader ( new FileReader(file));
BufferedWriter out = new BufferedWriter ( new FileWriter(temp,true));
while ((S=in.readLine())!=null)
{
String [] A= S.split(",");
if (A[0]!=(S1))//found CID
{
myList1.add(S);//append the course to the list
}
} in.close();
for(int i=0; i<myList1.size();i++)
{
out.append(myList1.get(i)+"
");
}
out.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(this,"Error Opening File: Check File Availability!");
}
boolean success = new File(file).delete();
if (!success) {
// Deletion failed
JOptionPane.showMessageDialog(this,"Error Deleting File!");
return;
}
boolean success1 = temp.renameTo(new File(file));
if (!success1) {
// File was not successfully renamed
JOptionPane.showMessageDialog(this,"Error Renaming File!");
return;
}
}
HEEELP!!!!
:
:
: thankssssssssss
:
An object of the type File represents a reference to a file, wether it already exists or not. It allows you to perform file functions, which you would normally do in the filemanager (such as windows explorer). Since the code copied the data into a new file, you can safely delete the old file and replace it with the new file. If you don't delete it, the rename call will fail since the file already exists.
You can use any object you like in any method you like as long as both share the same scope or if the object has a higher scope than the method.
: plz the last thing...with the method below pgram runs but nothing is done:
:
:
: ArrayList myList1;
: myList1= new ArrayList(100);
:
: String S;
: String file;
: String S1= jTextField5.getText().trim();
: File temp = new File("tempCourses.txt");
:
: file="courses.txt";
:
: if (S1.equals("
"))
: {
: JOptionPane.showMessageDialog(this,"Enter Your ID Number!");
: return;
: }
:
: try{
: BufferedReader in = new BufferedReader ( new FileReader(file));
: BufferedWriter out = new BufferedWriter ( new FileWriter(temp,true));
:
: while ((S=in.readLine())!=null)
: {
: String [] A= S.split(",");
: if (A[0]!=(S1))//found CID
: {
: myList1.add(S);//append the course to the list
: }
:
: } in.close();
:
: for(int i=0; i<myList1.size();i++)
: {
: out.append(myList1.get(i)+"
");
: }
:
: out.close();
:
: }
: catch (IOException e) {
: JOptionPane.showMessageDialog(this,"Error Opening File: Check File Availability!");
: }
:
:
:
: boolean success = new File(file).delete();
: if (!success) {
: // Deletion failed
: JOptionPane.showMessageDialog(this,"Error Deleting File!");
: return;
: }
:
: boolean success1 = temp.renameTo(new File(file));
: if (!success1) {
: // File was not successfully renamed
: JOptionPane.showMessageDialog(this,"Error Renaming File!");
: return;
: }
:
: }
:
:
:
:
:
:
:
: HEEELP!!!!
:
This ofcourse does not delete the contents of the file, since it only duplicates all the records except the one to be deleted. For this to work you need to replace the old data with the new, not append the new to the old.
: : plz the last thing...with the method below pgram runs but nothing is done:
: :
: :
: : ArrayList myList1;
: : myList1= new ArrayList(100);
: :
: : String S;
: : String file;
: : String S1= jTextField5.getText().trim();
: : File temp = new File("tempCourses.txt");
: :
: : file="courses.txt";
: :
: : if (S1.equals("
"))
: : {
: : JOptionPane.showMessageDialog(this,"Enter Your ID Number!");
: : return;
: : }
: :
: : try{
: : BufferedReader in = new BufferedReader ( new FileReader(file));
: : BufferedWriter out = new BufferedWriter ( new FileWriter(temp,true));
: :
: : while ((S=in.readLine())!=null)
: : {
: : String [] A= S.split(",");
: : if (A[0]!=(S1))//found CID
: : {
: : myList1.add(S);//append the course to the list
: : }
: :
: : } in.close();
: :
: : for(int i=0; i<myList1.size();i++)
: : {
: : out.append(myList1.get(i)+"
");
: : }
: :
: : out.close();
: :
: : }
: : catch (IOException e) {
: : JOptionPane.showMessageDialog(this,"Error Opening File: Check File Availability!");
: : }
: :
: :
: :
: : boolean success = new File(file).delete();
: : if (!success) {
: : // Deletion failed
: : JOptionPane.showMessageDialog(this,"Error Deleting File!");
: : return;
: : }
: :
: : boolean success1 = temp.renameTo(new File(file));
: : if (!success1) {
: : // File was not successfully renamed
: : JOptionPane.showMessageDialog(this,"Error Renaming File!");
: : return;
: : }
: :
: : }
: :
: :
: :
: :
: :
: :
: :
: : HEEELP!!!!
: :
: This ofcourse does not delete the contents of the file, since it only duplicates all the records except the one to be deleted. For this to work you need to replace the old data with the new, not append the new to the old.
:
however i am appending to the tempfile and not to the original one. the temp is created and it is empty i append all the content of the list (which is the duplicate of all records except the one i deleted) hence it is cpying all records except for the one i wish to delete into a new temp file. i then delete the whole orifinal file and i rename the new one!!!
is nt that logical? i think that the problem is in calling the original file. the file is already existing it is called " courses.txt" and it contains data thus when i finished copying the needed data to the new file I should delete the original
boolean success = new File(file).delete();
so how would i say new File(file) while this file is already there!!! i should find a reference to it !!!!
am thinking of an alternative solution: what about overwriting the original file with the data in the temp??
thanks for your help
is nt that logical? i think that the problem is in calling the original file. the file is already existing it is called " courses.txt" and it contains data thus when i finished copying the needed data to the new file I should delete the original
boolean success = new File(file).delete();
so how would i say new File(file) while this file is already there!!! i should find a reference to it !!!!
am thinking of an alternative solution: what about overwriting the original file with the data in the temp??
thanks for your help
The File object makes a reference in the RAM memory to a file structure on the disk. It does [b]not[/b] create a file. You wanted to convert a String object into some kind of filename structure. The File object does that for you. The first line of the explanation on java.sun.com explains it all:
[code]
An abstract representation of file and directory pathnames.
[/code]
Source: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
This means not that new File("filename") creates the file "filename" on disk, but only a way of accessing it without needing to worry about the OS-dependent structures. Only if you call the File.createNewFile() is a new empty file created (See http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#createNewFile() ).
I cannot explain it better than this.
Your alternative can also work, but that leaves a temp file after the program quits. I'm not sure if you want that, and deleting that file would only shift the problem to the representation of that file using a File object.
Thanks a lot.