: maaan plzzzzzzzzzzzz i have written this code following ur instructions:
:
: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
: //enrolling a Student.
: String S,W,F;
: String file,file1,file2;
:
: String S1="\n"+jTextField1.getText().trim();
: String S2="\n"+jTextField2.getText().trim();
:
: String L=jTextField1.getText().trim()+","+jTextField2.getText().trim();
:
: if (S1.equals("\n") && S2.equals("\n"))
: {
: JOptionPane.showMessageDialog(this,"Enter CRN and your ID");
: return;
: }
:
: else if (S2.equals("\n"))
: {
: JOptionPane.showMessageDialog(this,"Insert CRN!");
: return;
: }
: else if (S1.equals("\n"))
: {
: 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+"\r\n");
: 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 \n 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:
: 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");
This way you are sure that the search code is correct, You can also detect errors in the
algorithm.