This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes assign faculty members to classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "assign faculty members to classes" Watch "assign faculty members to classes" New topic
Author

assign faculty members to classes

P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
I am currently working on a program that I have to assign each faculty member 4 classes. I am reading from two text files. One file has the name of faculty members and their preferences(classes they prefer to take) in them and the other file has only to classes and their sections. I need to go through the file with classes and sections and see if the class that the first professor wants is listed and if it is assign it to that faculty member but if it is not their, move to the next class listed in the preference file and keep going until that faculty member has at least 4 classes then move to the following faculty member. Can anyone assist me in coding. this is what I have so far:
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
I suggest you use more descriptive names for your variables than firstFile and firstFile2 ie use something like staff and courses so you know at a glance what type of data they hold.

Next, stop writing code and sit down with a pen and paper and write down in your native language how you would do this if given 2 pieces of paper, one with list of staff and one with a list of courses. And when I say write down I mean write down every single step you go through when comparing the lists and deciding if a course is for a particular staff member or not etc.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

I couldn't agree with Tony more.

One of my colleagues (Junilu Lacar) provided this great quote on another thread:
H. Abelson and G. Sussman wrote:Programs are meant to be read by humans, and only incidentally for computers to execute.

The whole point of a computer language is to make machine instructions readable, so if you use meaningless names just to save yourself a few carpels you're simply shooting yourself in the foot. Unfortunately, proper naming seems to be a sadly neglected part of programming classes these days.

I also agree with him about your approach. Programming is about thinking, not coding; so stop the latter until you've done the former.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
This is what I am trying to code:

If Teacher1's first preference = class in class text file
Teacher1 gets first class
else
Move to next class
If Teacher1 second preference = class in class test file
Teacher1 gets second class
else
Move to next class.....etc

The code will test each class that the faculty member prefers to take and see if it is in the class file. Can any one guide me on how to check the fist element in one file To see if it is in the other file?
Example: Teacher Preferences
Dr Michael CSC100 CSC120 CSC207 CSC220
Dr.Bhat CSC105 CSC120 CSC323 CSC434
Dr Albert CSC100 CSC130 CSC303 CSC305
Dr Kuu CSC100 CSC270 CSC371 CSC390

Example: Classes
CSC100
CSC110
CSC303
CSC210
CSC210
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
That's not a native language description of how you would do this yourself with a pen and paper, it's an attempt to write pseudo code.

You should be writing something like:

For each of the names on the staff list read the next name and the associated class preferences for this name.
For each of the given preferences look down the class list to see if it is there.
If it is there then ...

P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
Thank you for your comments: Here is what Im trying to do:

For the first faculty member check the class list and see if the first class that is preferred in his list is listed
If it is listed give him that class
Check to see if the second class he prefers is listed then give that class to him
(Do this for every faculty member)
If any faculty member prefers a class that is already taken by another faculty member then give that class to the first faculty member that asked for it
Then give that other faculty member a class that is listed next in the class list that has not been taken


Any more comments or coding would help a lot.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Ok that's better but:

1. What happens if a preferred class isn't listed at all.
2. Given that the order of looking at faculty members could change the classes they are assigned what order do you resolve their classes in. ie is it the order they are in the file, alphabetical or some other order?
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
Tony Docherty wrote:Ok that's better but:

1. What happens if a preferred class isn't listed at all.
2. Given that the order of looking at faculty members could change the classes they are assigned what order do you resolve their classes in. ie is it the order they are in the file, alphabetical or some other order?


1.If the class is not listed then i should print Class not listed
2. The order of faculty will not change, we will go in the order that is listed in the file
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
So now you have an accurate set of steps describing how you would solve the problem you can start to write your code.
This is a simple problem so you may want to write it as a monolithic block of code but personally I'd break it down into discreet actions and put them into separate methods.
You also need to look for repetition of instructions as this is an indication you may need a loop.
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
Tony Docherty wrote:So now you have an accurate set of steps describing how you would solve the problem you can start to write your code.
This is a simple problem so you may want to write it as a monolithic block of code but personally I'd break it down into discreet actions and put them into separate methods.
You also need to look for repetition of instructions as this is an indication you may need a loop.


My issue is I'm not clear on how to write that specific loop. I've been researching loops for two months and I'm still stuck. I was thinking about a while loop, but I'm not sure that's the best one to use. The hard part is figuring out how to go through the faculty file. The actual file looks like this:

Do I use positions to go through the file? I found something called FileContents but I could only use that successfully with one column in the text file. Any help would be great.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
My issue is I'm not clear on how to write that specific loop. I've been researching loops for two months and I'm still stuck.

Again, if you are not clear on how to proceed write out how you would do it ie

Whilst there are more lines read the next line
Split the line at the white spaces so we have 5 separate pieces of data
The first piece of data is the faculty members name
The next 4 pieces of data are the 4 preferred courses
...

When you turn this into code you have 2 choices either you can read in the file, store all the data in a List, then iterate over the list later to assign the courses or you can read in the file and assign the courses as you go. Equally you have the same choice with the course file, although in this case it would almost certainly be better to read it in and store or you would have to re-read the file for every name in the faculty member list.

I was thinking about a while loop, but I'm not sure that's the best one to use.

The general rule is if you know exactly how many iterations you need to make use a for loop (or for-each loop) else use a while loop.
If you are reading in a file then you generally don't know how many lines it will contain, you just keep on reading until the end of file is reached.
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
Ok here is what i have so far but I still need guidance. It doesn't do exactly what I need it to do but it works. I also cant close it unless I close eclipse all the way. The JFrame doesn't close with DISPOSE_ON_CLOSE or EXIT_ON_CLOSE. Can anyone help? Any advise is great.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

On line 118 of your posted code, you catch all exceptions, and hide them. You call e.toString(), but nothing else, so whatever is happening you can't see. Never hide your exceptions, always display them, log them, or at the very least print stack traces.

On line 119 you do this:

But your frame is not a javax.swing.JFrame, it is a java.awt.Frame. So you can't cast the frame to a JFrame just so you can call the setDefaultCloseOperation() on it. You should be just using all swing components in your class unless there is a compelling reason to use AWT. And if you use AWT then you should use only AWT. And you can never treat a class of one type as if it is a class of a different type (GUI or not).


Steve
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Please use Java naming standards - variable names should start with a lower case letter.

Why are you reading in both the course and faculty files and comparing their lines - why would they ever be the same and where in your native language description of how you would solve this problem do you do this. The whole point of writing out how you would solve this is so you have a template to follow.
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
This reads the first file and splits the line between the white spaces
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
ok, but you may want to do the following:
1. trim the line before splitting it to remove any spurious leading/trailing white spaces.
2. move the references to the course file to another section so you are just handling one thing at a time (ie faculty preference parsing).
3. Look at your Map declaration and decide if it will hold the data you will have ie for each staff name there will be 4 course choices.

The next stage in your code is to iterate over the array of data you have and add each line to the map. To do this you will have to extract the staff name from the array and do something else with the remaining pieces of data. And, finally, close the input stream of course.

You will notice you now have a discreet block of code that deals purely with reading and parsing the faculty preference file which has an input (the filename to read) and a single output (the map of parsed data) so you can move this block of code to a method called something like readFacultyFile which take a filename as a parameter and returns the map of data.
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
Tony Docherty wrote:ok, but you may want to do the following:
1. trim the line before splitting it to remove any spurious leading/trailing white spaces.
2. move the references to the course file to another section so you are just handling one thing at a time (ie faculty preference parsing).
3. Look at your Map declaration and decide if it will hold the data you will have ie for each staff name there will be 4 course choices.


Thank you, you are very helpful.Here is what I've done so far.


I am not sure what you mean by number 2. Are you saying remove the data from the name file and put them in another column in the course section file? If so any hits on how to do that?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
No, I meant only open the file input stream for the file you are reading in this piece of code. So move the other input stream declaration and creation to the point in the code where you will use it.
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
I am trying to save the data to a file but it's not writing to the output file.


This is the error I am getting..Any help?


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

First of all, never try to run a class in your IDE while you still have compiler errors. Fix those first.

Change tokens on line 13 to token - you're now trying to write all tokens during each iteration, which is probably a typo.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
P Willia
Greenhorn

Joined: Oct 08, 2012
Posts: 12
Thanks that helped. Now it only printing the last line in the text file and putting it in the output file, how do I get it to print all the lines in the output file?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
You are opening and closing the output file for every line you read in and so you are overwriting the file each time. Why are you even writing the tokens out to a file, you have just read them in from a file?

Surely you want to extract the staff members name and then their 4 choices and store this data somewhere sensible eg create a FacultyMemeber class which holds a name and a list of their choices and then create an instance of this class for each line read in and store it in a collection eg Arraylist. At a later stage you may want to add some additional features to this class such as the actual subjects they are assigned etc.

You could do the same thing for the courses file ie read it in, parse it and store the data in instances of FacultyCourse. Then when you have read in all the data from both files and have successfully parsed and stored the data internally you can compare the data to work out which course gets assigned to which staff member.

I suggest you tackle one thing at a time otherwise your code will get too confusing. So read in each file and store the data internally, then assign the courses and finally write out the assigned courses.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assign faculty members to classes
 
Similar Threads
Flagging duplicate lines when comparing the input from two seperate text files
Need to read and write data from file.
no such element? eek!
Not writing to new file...
file renaming...