1

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.

4
  • Maybe it's me, but I'm having a tough time understanding what your problem is and what you're trying to do. If you don't get a decent solution soon, consider 1) writing out your problem a little more clearly using paragraphs to separate main ideas and using clear easily understood sentences. 2) Show any and all error messages that you may be seeing. Commented Jan 1, 2013 at 15:55
  • Are you supposed to be creating a collection of Worker or Pass objects? If so, where are these collections? Also, I've removed the netbeans tag since your question really has nothing to do with netbeans. Commented Jan 1, 2013 at 15:58
  • Also consider showing some of your data file, enough so that we can better understand its structure. Commented Jan 1, 2013 at 15:59
  • @HovercraftFullOfEels well yes, according to a txt file i should create Worker and Pass objects and add some logs. Commented Jan 1, 2013 at 16:04

1 Answer 1

2

You are getting the missing symbol error because worker1Pass was defined in the first if statement and is visible only in the if context. That is why netbeans is complaining too, because the worker1Pass in the first if is not visible (and not used) anywhere else. You should declare worker1Pass outside of your if, at least, to be visible in the second if statement too.

Like @HovercraftFullOfEels suggested, if you need to maintain all the workers and the pass objects you can use a Collection or a List, your code will look something like:

List<Officer> workers = new ArrayList<Officer>();
List<Pass> passes = new ArrayList<Pass>();
for ( i=0; i < aryLines.length; i++ ) { 
  String phrase = aryLines[i];
  String div = "[ ]+";
  String[] tokens = phrase.split(div);
  Officer worker = null; // and from this point on use this variable to represent a worker
  Pass workerPass = null; // used to represent a pass
  if (tokens[0].equals("Officer:")){
     if (tokens[1].equals("1")){
        worker = new Worker("1","worker1",1);
        workerPass = worker1.getPass();
        workers.add(worker); // add the worker in the workers list
        passes.add(workerPass); // add the pass in the passes list

     } 
     //etc. for other workers                   
  }
Sign up to request clarification or add additional context in comments.

8 Comments

Well i would like all the declaration to be in a loop after the text scan.Dont know if im supposed to freely declare it without the text telling me to.
With this approach will the pass.method see the Pass object, or do i have to "point" it to the list.Because there will be if loops again. However thanx for the "new stuff"
@user1839880 Yes, if you will use: workerPass.workerEntry(entry); and workerPass.workerExit(exit);, instead of worker1Pass use the for visible workerPass object.
Think this was my problem.Thanx for the tips.Tell me one more thing, how does the list solve the problem? Is the list "visible" all the time by anyone?
@user1839880 You are welcome. The declaration of Officer worker = null; and Pass workerPass = null; are solving your issue. The list is needed only if you need to have access to all the workers and their passes, after the file was processes (the loop has finished).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.