• Post Reply Bookmark Topic Watch Topic
  • New Topic

ID Numbers are All 0  RSS feed

 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what I did to screw this up, but I had this code working to increment each employee ID by one every time a new employee was added. I tried to move the whole employeeID and newEmployeeID elements out of the constructor because everyone keeps telling me how bad it is to have those in the constructor. However, now, every time I add an employee, their ID number turns out to be 0. I am desperate at this point to figure this out. I have two other aspects of this project that are due soon, and if I can't fix this part, I won't be able to move forward to the other parts (which I am also stuck on at this point). The codes that are being used are as follows:

AND:

Any help is greatly appreciated!
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look very carefully at line 27 in Employee class. Can you tell us what exactly is it doing? Consider variable names hiding.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I don't think the no‑arguments constructor is a good idea. Do you want employees with no names? And you are automatically passing 0 to the other constructor. If I were writing that class, I would remove that constructor altogether.
You might have been told to provide a no‑arguments constructor, in which case maybe you should redesign it.
 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Look very carefully at line 27 in Employee class. Can you tell us what exactly is it doing? Consider variable names hiding.

Well, at first, I had only the employeeID part in the constructor. However, when I did, I could not get the employeeID to increment at all. At one point, the current coding was incrementing and assigning sequential ID numbers to the employees that I entered. If I had any idea what I was doing wrong, I would try to fix it. However, I am completely at a loss. What I want, ultimately, is for the employeeID number of the employee the user is entering into the console to be the next sequential ID available (newEmployeeID). Apparently, I'm doing it wrong.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should answer the question that you were asked first. What do you think line 27 is doing?
 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:You should answer the question that you were asked first. What do you think line 27 is doing?

I think that line 27 is making the current employeeID equal the newEmployeeID, which should be incrementing via newEmployeeID++. Is that not right?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anakela Bella wrote:
E Armitage wrote:You should answer the question that you were asked first. What do you think line 27 is doing?

I think that line 27 is making the current employeeID equal the newEmployeeID, which should be incrementing via newEmployeeID++. Is that not right?


But you have two employeeIDs at that point so which one is being incremented?
See the lines above that where you do this.firstName = firstName; . Now compare those lines with line 27.
 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:
Anakela Bella wrote:
E Armitage wrote:You should answer the question that you were asked first. What do you think line 27 is doing?

I think that line 27 is making the current employeeID equal the newEmployeeID, which should be incrementing via newEmployeeID++. Is that not right?


But you have two employeeIDs at that point so which one is being incremented?
See the lines above that where you do this.firstName = firstName; . Now compare those lines with line 27.

Oh. My. GOODNESS! It's always something really small that always eludes me. I just had to add "this." to the "employeeID = newEmployeeID;" and everything was fine. Thank you for your help! I cannot believe I did (or did not do, I guess) that.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It confuses everybody when they start.
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anakela Bella wrote:I just had to add "this." to the "employeeID = newEmployeeID;" and everything was fine. Thank you for your help! I cannot believe I did (or did not do, I guess) that.

I'm glad you were able to solve this .
So your constructor currently looks like this?So, as it is, the constructor parameter employeeID does nothing. It is unused. Do you need it?
 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Anakela Bella wrote:I just had to add "this." to the "employeeID = newEmployeeID;" and everything was fine. Thank you for your help! I cannot believe I did (or did not do, I guess) that.

I'm glad you were able to solve this .
So your constructor currently looks like this?So, as it is, the constructor parameter employeeID does nothing. It is unused. Do you need it?

Yes, that is exactly what my new constructor looks like! I think I am going to need the employeeID parameter later in the project when I attempt to read the employeeID from a text file. If not, I will be sure to get rid of it, though I am nervous about doing so because I run into problems like forgetting to use the this. operator. Thanks again!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you get rid of the no‑arguments constructor. You seem to have a good case for overloading the constructor, to take the ID from the text file, but there is now the risk of having two Employees with exactly the same ID.
Don't call this. an operator. The . is an operator (member access) but this is a keyword referring to the current object. It is a little bit like null which is a literal.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!