Question

I am doing an assignment in Eclipse Indigo with Java. I have to create a program for a school and that program is a spelling test program. I have been able to allow staff & students to log on and also change their passwords and edit their accounts. I have also been able to create tests and also access them to edit. But one thing I cannot do is access the available tests in a list to see how many tests have been set. I don't know what to change in my code to fix this problem. To display the tests I have created a method called displayTest. I have created storage for the tests using a HashFile.

Here I have saved storage for the tests in a HashFile.

String test_FILENAME = "datafiles/test.dat";
HashFile testFile = new HashFile(test_FILENAME, 100, new Test());

I have continued to add a test ( I have storage for the variable test at the top of my coding document) using:

public void addTest() {
   Test test = new Test();// create storage for a teacher
   test.input(); // read the teacher details in through the keyboard
   testFile.store(test); // store the teacher in the teacher file

I have also been able to edit the account (the PC etc. is from another class I use) using:

public void editTest() {
  String testNo;
  PC_Dialog editTests = new PC_Dialog("Edit Test", "Test No", "Find");// create a dialog box to ask for the teacher number
  editTests.choice();// display the dialog box
  testNo = editTests.getField(21); // get the teacherNo that has been entered
  test = (Test) testFile.retrieve(testNo); // retrieve the teacher from the file
  if (test != null) {// check if the teacher is found
     test.edit();// yes so display it for editing
     testFile.store(test); // store the teacher back in the teacher file
  } else
     JOptionPane.showMessageDialog(null, "Unable to find the test!");// not found so give an error message
}// end of method

But in displayTest() I have some problems. The displayTest is meant to allow the teachers to view all the tests set to see which ones to edit and which ones remove at the click of a button. But the problem is that the if (test.year == year) { has a red line underneath the first year and when I hover over it, it says 'year cannot be resolved or is not a field'. Can anyone help me try and make the displayTest section collect tests from the data file and display them on a table? I am new to Stackoverflow and have only used it once over a year ago. I apologize if anyone becomes frustrated looking at a newcomers work but this is practically like my first time using Stackoverflow. Help is much appreciated, thanks.

I HAVE CHANGED THE CODE SINCE:

public void displayTest() {
  Test testNo = new Test();
  Test yearGroup = new Test();
  Test date = new Test();
  PC_Table table = new PC_Table("Tests Available", 0, "Test Number, Date, Year Group", "OK");
  int row = 1;// a counter for the current row
  // prepare the file for serial access
  testFile.resetSerialAccess();
  // loop through all the records in the event file
  while (testFile.moreSerialRecords()) {
     // read the next record
     Test test = (Test) testFile.getNextSerialValue();{
        // retrieve the reason
        // add a row to the table and put the values in
        table.addRow();
        table.setValueAt(test.testNo, row, 1);
        table.setValueAt(test.date, row, 2);
        table.setValueAt(test.yearGroup, row, 3);
        row++;
  }
  table.choice();
  }

 }// end of method

Nothing from the test database (test class) is showing on the table.

Was it helpful?

Solution

You have

Test date = new Test();

"Test" is the name of the class. "date" is the name of instance of that class. You say the error is in

if (test.year == year)

I don't see that line in the code, but what you want is

if (date.year == year)

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top