Campbell Ritchie wrote:Welcome to the Ranch
I know I seem stupid to say this, but I can't understand the problem. You obviously have 31 jobs (how do you know there are 31 of them?). You are reading from the file and splitting each line with another Scanner. Good.
Why are you using if rather than while?
What is supposed to happen? What goes wrong? What is your design?
Joanne
Joanne Neal wrote:At the moment, in your readJob method you are reading all the job details into a local array. This array will go out of scope as soon as you return from the method, which means you will lose all the data you have just read in.
In your menu method, you create a new Job object and then check if its day matches the day just typed in by the user, which of course it doesn't because the default value for day is null.
Assuming these two methods are in the same class, what you need to do is to read the data into an array that is referenced by a field (or instance variable) of the class and then loop through this array in your menu method checking for Job instances that match the entered day.
Joanne
Joanne Neal wrote:In your Job class on lines 3 - 9 you have a number of variable declarations that are inside the class defintion but not inside any method - these are known as fields or instance variables. They are accessible from any non-static method within your class.
In your readJob method you declare and initialise an array on line 13. The jobArray variable is declared inside the method which means it is a local variable and is only accessible within that method. To be absolutely precise it is only accessible within the if statement that starts on line 9, but don't worry about that for now.
So, do you think you can change jobArray from a local variable to an instance variable ? If you need more information take a look at The Java Tutorial.
You seem to understand for loops (you have one on line 15 of your readJob method) and you seem to understand how to check if the day value of a Job instance matches the user input (you do this on line 10 of your menu method), so you just need to combine the two.
Have a go at that and then come back with your new code if you have any problems.
Ashleigh Barbara wrote:So I added that to the Job class
Joanne
Joanne Neal wrote:
Ashleigh Barbara wrote:So I added that to the Job class
The instance variables I referred to in your Job class were just as an example of where they need to be defined in the class definition (outside of all methods but within the class declaration). The jobArray variable needs to be declared in the same class as your menu and readJob methods. Sorry if that wasn't clear. But you're right to put the creation of the array in the constructor - it just needs to be in the constructor of your class, not the Job class.
Ashleigh Barbara wrote:What can I put instead of = i as it's telling me it can't convert from int to Job?
Joanne
Joanne Neal wrote:
Ashleigh Barbara wrote:What can I put instead of = i as it's telling me it can't convert from int to Job?
You don't need to initialise your array in the main method. You do that in your readJob method (lines 15 - 63).
Carey Brown wrote:Line 13 - you are still declaring a local variable 'jobArray', this means your class instance variable jobArray will be ignored.
Ashleigh Barbara wrote:So I delete that line, do I do anything else to readJob() to make it populate the instance variable?
Joanne
Joanne Neal wrote:
Ashleigh Barbara wrote:So I delete that line, do I do anything else to readJob() to make it populate the instance variable?
Yes.
At the moment, that line both declares a variable (indicates to the compiler what type it is) and initialises it (sets it to point at an object).
But you've moved the declaration to line 2 of your Automotive_Services class and you've moved the initialisation into the constructor (line 22), so you don't need that line.
However, you're now going to get an error saying something like instance variables cannot be accessed from a static context. This is because you've made all your methods static - remove the static modifier from all of your method signatures and you will be a step closer. Now you just need to work out how to call these non-static methods from your main method. As a clue - notice that all the methods in the Job class are non-static, yet you are calling them without problem from your readJob method - what's different between how you are calling the Job class methods and how you are calling the Automotive_Services class methods ?
Ashleigh Barbara wrote:I call the Job class methods using getters. Do I have to do the same thing to the main class methods?
Joanne
Ashleigh Barbara wrote:I created a setter and a getter for readJob(). Is this correct?
Joanne Neal wrote:
Ashleigh Barbara wrote:I call the Job class methods using getters. Do I have to do the same thing to the main class methods?
I'm not sure what you mean by a getter. Most people here would think of a getter a s a method that returns information about the state of a class instance (as opposed to a setter which sets the state of a class instance), so saying "I call the Job class methods using getters" doesn't really make much sense.
To call a non-static method, the compiler needs to know which instance of a class you want to call that method on. It's like telling a doctor to amputate a patient's leg - the doctor's not going to chop off every patient's leg - he's going to ask which patient's leg needs removing.
If you look at lines 48 - 54 of your readJob method you are setting various bits of information about a Job instance. How does the compiler know which Job instance to set those values on. You don't want to set those values on every Job instance (which is what a static method would do), so can you see how the call to setJobID, setCustomerID, setRegistration, etc is applied to a specific Job instance ? Compare that to how you are calling your readCustomers, readServices, readVehicle and readJob methods in your main method and see if you can work out what you need to change.
Ashleigh Barbara wrote:For each iteration of the for loop a new Job currentJob = new Job(); is called which breaks up the line of text using the delimiter and stores the values for each Job instance in the 31 array elements. Do I put the readJob etc methods into a loop of some kind? Also, do I need to do anything else to the contents of readJob() after I removed Job[] jobArray = new Job[31]; ?
Joanne
Junilu Lacar wrote:Coming in with fresh eyes, it seems to me everyone has gotten hung up on the nitty-gritty detail of the jobArray variable.
Joanne
Junilu Lacar wrote:Coming in with fresh eyes, it seems to me everyone has gotten hung up on the nitty-gritty detail of the jobArray variable. I'd like to back you up and ask you to StopCoding (←click on that link) for a minute, forget about jobArray, and think about a few basic things first:
1. Will you always have 31 jobs? This is a text file and as such I would not assume that it will only ever have 31 lines in it. Besides, it's just as easy to write the program without making this assumption.
So, if we don't assume that there will always be 31 jobs, then an array with a fixed length is not the appropriate data structure to use here. You need something that's flexible, something that can grow, like a java.util.List. As you read each line from the data file and create a Job from that line (more on this in a bit), you would use the List.add() method to add the new Job to your list. From here, there are all kinds of things you can do with the collection of Jobs.
2. Examining the sample data that you gave, I see that each line contains information about one job. That's good. I see that each field is separated from the next by a comma. That's good. I see that the first five fields on each line are consistently Job Id, Customer Id, Registration, Date, and Day. That's great. Now here's where we find two monkey wrenches. First, the number of fields provided after Day is NOT consistent. There are anywhere from two to five additional fields in the sample. Next, the Fee fields can be either missing, as indicated by two commas in succession as in lines 13, 16, 21, and 27, or not consistent specified with a decimal. Can you safely assume that the value for Fee will only either be there or missing? That is, can we always assume that if it's not missing, the number after the Day field will always be the Fee?
If the rule for Fee can be stated as: "The number after Day is always the Fee. A missing Fee value, denoted by two commas after the Day, should be taken as 0.0" then you're good to go.
3. After the Fee, there are anywhere from one to four Service Codes in the sample data. Is it safe to assume that there is at least one Service Code and that in general there can be 1 to N Service Codes that make up the rest of the line? No other kind of values will follow Service Code, right?
If I'm on track with all of the above, this is how I recommend you solve this:
1. Declare a List as the container for your jobs. Name this variable something logical, like say, oh... jobs . As the implementation class, you can use an ArrayList. So your declaration would be like this:
List<Job> jobs = new ArrayList<Job>(); or if you're using Java 7 or later, List<Job> jobs = new ArrayList<>();
2. For each line you read in from the text file, use String.split() to create an array that will contain all the field information. Pick up the values that are consistently provided in the data file from this array and assign them to the appropriate field of a new Job object. Then split off the last part of the array with all the Service Codes and keep that as the data structure that you put in the Job object's Service Codes field. You can use java.util.Arrays.copyOfRange() to do this. For example,
Joanne Neal wrote:
Junilu Lacar wrote:Coming in with fresh eyes, it seems to me everyone has gotten hung up on the nitty-gritty detail of the jobArray variable.
Actually we're hung up on how to call a non-static method. Introducing all this new information at this point will be counter-productive in my opinion, but let's see which way Ashleigh wants to continue.
Joanne Neal wrote:Introducing all this new information at this point will be counter-productive in my opinion, but let's see which way Ashleigh wants to continue.
Junilu Lacar wrote:
Joanne Neal wrote:Introducing all this new information at this point will be counter-productive in my opinion, but let's see which way Ashleigh wants to continue.
Just thought I'd offer a fresh perspective. Sometimes it helps me when I back away from a problem far enough to see the bigger picture. Looks like Ashleigh is ready to continue the way you guys were going though, so carry on, I guess. (exiting quietly to the side)
Ashleigh Barbara wrote:I've managed to figure out how to call the non-static methods yay. That seemed to do the trick. Am I now able to populate the instance variable array with readJob()?
Joanne
Ashleigh Barbara wrote:Thank you for your input though. Universities like to set rules that make these projects way more difficult than they need to be.
Joanne Neal wrote:
Ashleigh Barbara wrote:I've managed to figure out how to call the non-static methods yay. That seemed to do the trick. Am I now able to populate the instance variable array with readJob()?
Well done. Assuming your code all compiles okay now, then running it should populate the array - assuming there are no logic bugs of course.
Joanne
Joanne Neal wrote:Seems like a good idea.
Ashleigh Barbara wrote:I apologise if I keep asking silly questions but I'm not quite sure how to write the if statement
The only obstacle is i can't be resolved to a variable
Joanne
Joanne Neal wrote:
Ashleigh Barbara wrote:I apologise if I keep asking silly questions but I'm not quite sure how to write the if statement
The only obstacle is i can't be resolved to a variable
i is the loop variable of the for loop that you haven't added. You're trying to check every Job in your array to see if it matches the entered day. So you need to loop through your array.
Campbell Ritchie wrote:I think all the work Joanne has put into this thread merits a cow.
Joanne
I do some of my very best work in water. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|