Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help help>>Zibadian

tollitolli Posts: 44Member
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
«1

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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
    :
    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.
  • tollitolli Posts: 44Member
    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="
    "+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
  • zibadianzibadian Posts: 6,349Member
    : 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="
    "+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.
  • tollitolli Posts: 44Member
    thanks man for your hint. am trying it now.

    I apologize for taking a lot of your time
  • tollitolli Posts: 44Member
    I hope this would be my last request coz i took a lot of ur 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
  • zibadianzibadian Posts: 6,349Member
    : I hope this would be my last request coz i took a lot of ur 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
    :
    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.
  • tollitolli Posts: 44Member
    am srry again:

    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;
    }
    */
  • zibadianzibadian Posts: 6,349Member
    : am srry again:
    :
    : 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]
  • tollitolli Posts: 44Member
    but the file "courses.txt" i have it already created with date inside it...!!!!!!!! i need to use these data and process them as in the code....another thing, can I call an ArrayList and its methods from within a method?


    thankssssssssss
  • tollitolli Posts: 44Member
    [b][red]This message was edited by tolli at 2007-3-13 2:37:31[/red][/b][hr]
    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!!!!
«1
Sign In or Register to comment.