So after completing the last assignment I was given, I was instructed to take that code and change it from command line arguments to reading in data from a file. That all works out well, except for the part where I’m supposed to have an interface for a function that calls in the data from the file, then does the same things as before.
Right now, the object array in my driver class is SUPPOSED to be assigned the values taken in by the DAO class. The DAO class is based off the interface. The driver class is screaming at me that the object I have created has to be assigned from a static function in the DAO class, but that method can’t be static…
What did I miss this time?..
Interface:
public interface ScanTextFile {
public Object[] readTextData() throws FileNotFoundException;
}
DAO Class:
public class StudentDAO implements ScanTextFile {
public Object[] readTextData() throws FileNotFoundException {
Student[] studentRecord = new Student[3];
String dataFileName = "data.txt";
int numberOfRows = 0;
File dataFile = new File(dataFileName);
Scanner scan = new Scanner(dataFile);
int i = 0;
String delim = "\\|";
// checks number of rows in data file, making sure there are 3 total
for(i = 0; scan.hasNextLine(); i++){
numberOfRows++;
}
if(numberOfRows < 3){
System.err.format((numberOfRows) + " argument(s) - expected 3");
System.exit(0);
} else if(numberOfRows > 3){
System.err.format((numberOfRows) + " arguments - expected 3");
System.exit(0);
}
for(i = 0; i < numberOfRows; i++){
if(scan.hasNextLine()){
String temp = scan.nextLine();
String[] tempData = new String[4];
Student tempStudent = null;
for(i = 0; i < tempData.length ; i++){
tempData = temp.split(delim);
}
System.out.println("DEBUG *** Finished extracting data, creating object...");
System.out.println("DEBUG Student Data = [�" + temp + "]");
GregorianCalendar date = new GregorianCalendar();
try {
date = DateUtil.convertFromDMY(tempData[3]);
} catch (ParseException e1) {
e1.printStackTrace();
}
tempStudent = new Student(tempData[0], tempData[1], tempData[2], date);
studentRecord[i] = tempStudent;
}
}
return studentRecord;
}
}
Driver Class:
public class Lab3 {
public void main(String[] args) throws ParseException, FileNotFoundException{
Student[] allData = new Student[3];
allData = (Student[]) StudentDAO.readTextData();
System.out.println("");
System.out.println("DEBUG *** Student Objects created, displaying all Students...\n");
for(Student s : allData){
Print.print(s);
}
}
}
edit
Thanks for pointing out that error, thanks everyone, but now I’m getting
Exception in thread “main” java.lang.NoSuchMethodError: main
Is that because StudentDAO has no main?
another edit
@mprabhat thanks for pointing out a really stupid error, still don’t know how I didn’t see that ><
Now I have an issue when the scanner attempts to read the data in from the file.
1 – says data file can’t be found, even though it’s in my src folder.
2 – error on the scanner line as well, should I not be using a scanner on a file? should I be going with … DataInputStream?
Your method
readTextDatais not static but you are accessing it like a static method by using class nameStudentDAOInstead create an object
StudentDAOand then callreadTextDataIssue in your Lab3 is that you dont have correct signature of your main method.
public static void main(String[] args)is the correct signature, your signature is missingstatic, hence you are gettingjava.lang.NoSuchMethodError: main