I have a class named worker and a class called Pass which is the pass of the worker.
Im trying to built the main class in which all the processes are read from a text file.My code is full of loops and hopefully will work, but im having a problem.The first line of the text is plain text something like Worker: 1 worker1 , which simply means create a Worker object named worker1 and with passid (the id of his pass) number 1.This is do’able.However after this i simply put in the loop the creation of his pass.Which is my problem.
The 2n line of the text file gives my an AccessLog for the worker1 giving me a date of entry and exit.
public static void main(String[ ] args) throws IOException {
String file_name = "data.txt";
try {
FileRead file = new FileRead( file_name );
String[ ] aryLines = file.Openfile();
int i;
for ( i=0; i < aryLines.length; i++ ) {
String phrase = aryLines[i];
String div = "[ ]+";
String[] tokens = phrase.split(div);
if (tokens[0].equals("Officer:")){
if (tokens[1].equals("1")){
Officer worker1 = new Worker("1","worker1",1);
Pass worker1Pass = worker1.getPass();
}
//etc. for other workers
}
Here comes the code for the access log and will explain the error
else{
if (tokens[0].equals("AccessLog:")){
if (tokens[1].equals("1")){
String s1 = tokens[2] + tokens[3] ;
String s2 = tokens[4] + tokens[5] ;
try{
Date entry = new SimpleDateFormat("dd/MM/yyHH:mm:ss").parse(s1);
Date exit = new SimpleDateFormat("dd/MM/yyHH:mm:ss").parse(s2);
worker1Pass.workerEntry(entry);
worker1Pass.workerExit(exit);
}
catch (ParseException a) {
System.out.println( a.getMessage() );
}
}
workerentry and exit are methods in the pass class that log the access, and i get an error for missing symbol worker1pass.Also there seems to be a problem in the workerpass declaration because in netbeans there is a grey line beneath worker1pass calling it as an unused object.Lastly i would like to add that except the workaround code for the external text file the code works 100%.
Any tips and suggestions are welcome.
EDIT——-
Worker worker1 = new Worker("1","worker1",1);
Pass worker1Pass = worker1.getPass();
under worker1Pass im getting : variable worker1Pass is not used
worker1Pass.workerEntry(entry);
worker1Pass.workerExit(exit);
Here im getting cannot find symbol : worker1pass
Is like the pass worker1pass declaration does not declare a new pass as it should do and thus the methods don’t add the entry and exit to the log as they should do.
You are getting the
missing symbolerror becauseworker1Passwas defined in the firstifstatement and is visible only in the if context. That is why netbeans is complaining too, because theworker1Passin the firstifis not visible (and not used) anywhere else.You should declare
worker1Passoutside of yourif, at least, to be visible in the secondifstatement too.Like @HovercraftFullOfEels suggested, if you need to maintain all the
workersand thepassobjects you can use a Collection or a List, your code will look something like: