• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need some serious help  RSS feed

 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have iterated a text file into 31 sets of getters and setters in this piece of code
And I want to be able to type in a day and have all the lines of the text file that contain that day show the corresponding getters.
This is what I have started with

I have absolutely no idea of what else to do.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


I need to be able to input a day for example "Wednesday" and have every array index that has its getDay() set to Wednesday to print.

This is the text file I'm reading from



This is the Job class

If I put a while loop it throws a no line found exception and I don't know how to fix it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Would you be able to show me how to do this?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


So I added that to the Job class, do I move the array initialiser from readJob() in the main class to Job?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Ok I've done the following

What can I put instead of = i as it's telling me it can't convert from int to Job?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

Oh ok, so I don't put anything. So I leave readJob() exactly how it is? Thanks a lot for your help by the way
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still having a bit of trouble, if anyone else could help that would be great.

I'm not sure what to change here to get it to correlate with the following
 
Carey Brown
Saloon Keeper
Posts: 3317
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 13 - you are still declaring a local variable 'jobArray', this means your class instance variable jobArray will be ignored.
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Line 13 - you are still declaring a local variable 'jobArray', this means your class instance variable jobArray will be ignored.

So I delete that line, do I do anything else to readJob() to make it populate the instance variable?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

I call the Job class methods using getters. Do I have to do the same thing to the main class methods?
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a setter and a getter for readJob(). Is this correct?

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore this one






Ashleigh Barbara wrote:I created a setter and a getter for readJob(). Is this correct?

 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


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]; ?
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]; ?

Each iteration through that loop creates a new Job instance (Job currentJob = new Job();) and then sets the values for that particular Job instance (currentJob.setJobID(jID); , etc). You repeat this 31 times so you end up with 31 instances of the Job class, which you store in your array.
But you only need one instance of your Automotive_Services class, so you don't need a loop. You are already creating the Automotive_Services instance (do you know which line is doing that ?), so all you need to do now is call the readCustomers, readServices, readVehicle() and readJob methods on that instance.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,



I won't know if there will always be 31 jobs but I'm not allowed to use array lists. I've gotten it to read empty fee fields as 0, that's not a problem and yes it will always be fee after day. The rest of the numbers after the fee are services codes which are put in their own array and that works fine too. Yes, no other value will follow the service codes.
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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()?
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

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
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Not a problem. Personally, I find it ridiculous the way it seems kids are being taught in universities nowadays. Admittedly, it's been a loong time since I graduated but I don't believe in holding people back just because you have to cater for the least common denominator or even worse, because you haven't gotten to that slightly more advanced topic yet. I thought college was supposed to be where you free your mind and explore boundless opportunities. To be constrained to using an array for this kind of problem is ... I don't even have the words.

Anyway, good luck with your problem and your course. You're in good hands with Joanne and the others who have been helping you so far. Later.
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

So should I try inputting a day in the menu() method now?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like a good idea.
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Seems like a good idea.

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 Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashleigh Barbara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ah of course. It works perfectly. Thank you so very very much for your help
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think all the work Joanne has put into this thread merits a cow.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think all the work Joanne has put into this thread merits a cow.

Muchas gracias.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!