0

I've been trying to write a method in java to write data from an array to a text file but I'm getting two errors.

public void WriteStudentDetailsToFile() {
  PrintWriter out = null;
  try {
    out = new PrintWriter("StudentDetails.txt");
  } catch (FileNotFoundException ex)  {
    System.out.println(ex.getMessage());
    System.out.println("in " + System.getProperty("user.dir"));
    System.exit(1);
  } 


  for (int i = 0; i < enrolment.length; i += 1) {
    if (enrolment[i] != null) {
        Student a = this.enrolment[i];
        if (a.getName().equals("") || a.getAddress().equals("") || a.getDOB().equals("") || a.getGender().equals("")) {
            break;
        } else {
            String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();
            out.println(record);
        }
     }
   }
 }
}

The two errors are

C:\Users\B00661059\Downloads\Assignment 2\Assignment 2\Student_Enrolment.java:137: error: ';' expected String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();

^ C:\Users\B00661059\Downloads\Assignment 2\Assignment 2\Student_Enrolment.java:137: error: not a statement String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();

0

3 Answers 3

1

On line 137, you need to add a concatenation operator (+) here:

"\t" + a.getDOB()
Sign up to request clarification or add additional context in comments.

Comments

1

The most obvious error is that you are missing a plus here: + "\t" a.getDOB() +.

It should be

String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" + a.getDOB() + "\t" + a.getGender();

You might also want to look into how to define and use a toString() function in your Student class to control the string representation of the object.

Comments

0

Try this :

String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" + a.getDOB() + "\t" + a.getGender();

instead of this :

String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();

Also this doesn't make sense :

if (a.getName().equals("") || a.getAddress().equals("") || a.getDOB().equals("") || a.getGender().equals("")) {
        break;
    } 

Since you are only interested in the else branch you can negate it.

if !(a.getName().equals("") || a.getAddress().equals("") || a.getDOB().equals("") || a.getGender().equals("")) {
        // Do what you want
    } 

Comments