This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Having trouble with strings

 
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This always produces "she", "her" no matter what gender is.



When I do it here, it works. I don't understand what the difference is.

 
Greenhorn
Posts: 24
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you assign the value to string during run time , that in effect create a new string object on the heap which the reference variable gender now points to. When you say if gender == "Male" , its actually comparing 2 different object. The "Male" is a string literal and stored separately. So this would return false. Since you are actually comparing 2 different string objects stored in different locations.

In the second example , you assign the value to string in the code (String gender = "Male";). This is a string literal assignment and all those literals are stored in the string pool. Later on in the code when you are checking  gender == "Male" , that is again a check against a hard coded string literal "Male"....all these literals in the code are all kept in the string pool and as long as they are same value, only such object is present . So your check gender == "Male" results in true .Since they are both pointing to same object.

If you wanted to compare the value in the string , you should use the equals method (gender.equals("male") or gender.equalsIgnoreCase("male") if you don't care about case). This actually compares the value stored by the object. The == compares the address where the string objects resides.
 
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use a text field for gender; what will happen if somebody writes “M” or “F”?
I can't remember offhand which component you would use for a restricted range of options, but maybe a combo box, maybe a  JList.
Also, I would warn against storing gender as a String in the first place. Find out how to write an enumerated type (also called an enum)and how to populate your GUI from that enum. Also find out how to use the valueOf() method to convert text to Gender.

You are allowed to use the == operator on enum elements
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you shorten your if‑elses with the ?: operator.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't use a text field for gender; what will happen if somebody writes “M” or “F”?
I can't remember offhand which component you would use for a restricted range of options, but maybe a combo box, maybe a  JList.
Also, I would warn against storing gender as a String in the first place. Find out how to write an enumerated type (also called an enum)and how to populate your GUI from that enum. Also find out how to use the valueOf() method to convert text to Gender.

You are allowed to use the == operator on enum elements



Yeah, I was going to do a JSpinner with just the two options "Male" and "Female" but trying to figure out how to get the value from the jspinner, pass it to the DAO and then retrieve that value from the DB back into the "Update Office" dialog was way beyond me. I decided to try to do a string because I am alittle more proficient with them than getting component values. I wanted to just see the email templates change according to gender, and then set about learning to do the same thing with a JSpinner to better control the input.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, but I don't think a spinner is the best component for this problem.
Familiarity with Strings doesn't mean that Strings are the best solution. There is nothing to stop you writing,, “Feemale,” (as I did earlier). That problem won't occur with an enum. What's more, you can use a similar construct in your SQL data definitions, but you should make sure to document that so client code can create corresponding enums.
Remember that once you have let such documentation out into an unsuspecting world, it is very difficult or impossible to change the database's public interface.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Not sure, but I don't think a spinner is the best component for this problem.
Familiarity with Strings doesn't mean that Strings are the best solution. There is nothing to stop you writing,, “Feemale,” (as I did earlier). That problem won't occur with an enum. What's more, you can use a similar construct in your SQL data definitions, but you should make sure to document that so client code can create corresponding enums.
Remember that once you have let such documentation out into an unsuspecting world, it is very difficult or impossible to change the database's public interface.



Yeah, I am still learning core but playing with this project with all the rest of my spare time. I have yet to learn enums. I will have to look at those next.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "equals/equalsIgnoreCase" worked like a charm for the string code. Enum sounds like the way to go though. My biggest challenge is using it with a dialog: "AddOffice.java" That requires an OfficeDAO and an Office.java.

My Office object is one int and the rest Strings:

which I am familiar with this pattern. I have expanded on it from 8 attributes to now 17 with little to no trouble. I have added a CSV export and email template creator classes for the offices.
It functions but I know my code could be better written and as I learn more, I make the changes. Now, I have enums. A brief look at oracles tutorial gave me pause as I can't at this point, see how to
incorporate it accross the classes that will need to access it's value. I have my work cut out for me.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your dialogue window should only implement a display. In the case of a dialogue, that will be short‑lived. It simply requests you enter the appropriate information. Other code will verify that information, for example verifying that a zip code has five digits (or five digits hyphen four digits or whatever the correct format is), and ask you to correct any mistakes before allowing the dialogue to close.
I suspect that status would be better as an enum, too. Make your enums immutable, so their constants can be shared safely. I think the intention behind enums is that they should always be immutable.

Remember you can turn enums into Strings with toString() and vice versa with valueOf().
 
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ray Gilbert wrote:My Office object is one int and the rest Strings:




See Long parameter list code smell and refactor long parameter list to builder
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just worked out what subPron and obPron mean. Which means the names aren't clear. They are unnecessary parameters, too, if they can be extracted from the Gender object. That will redue the parameter list by two.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have just worked out what subPron and obPron mean. Which means the names aren't clear. They are unnecessary parameters, too, if they can be extracted from the Gender object. That will redue the parameter list by two.



I don't know how, yet, to make the parameters of my Office smaller and still do this:

Watch Demo: OmniTrack
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say Gender obj, do you mean the enum? And, do I need to make each of these parameters a class of their own or group some of then into another class? Forgive my idiocy, I am still learning but trying to code above my level by quite a bit.
 
Marshal
Posts: 6670
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't have one class for one parameter, but for instance, you could refactor out the Office address into an Address class.  This could be reused for any sort of address.  I think four or five fields per is about the maximum.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ray Gilbert wrote:When you say Gender obj, do you mean the enum? . . .

Yes.

What idiocy? I can see no idiocy/ You are confronting new code and new ways of coding; you can expect them to be unfamiliar.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ray Gilbert wrote:When you say Gender obj, do you mean the enum? . . .

Yes.

What idiocy? I can see no idiocy/ You are confronting new code and new ways of coding; you can expect them to be unfamiliar.



Thank you Campbell, you are very kind.

My story is this, and maybe will explain why I am pushing so hard to be developer. I have always wanted to learn to code but never made the jump for one reason or another. I am now almost 51 y.o. I was injured and disabled in 2011 in the Army. Two years ago, I died, clinically due to a number of blockages in my left ascending artery. I recieved stents and am doing well but I have seen my mortality. I'm tired of feeling disabled, I want to provide for myself and my family. I really love coding, despite the fact that it is dirty as hell right now. I know it is and every opportunity to learn to make it better excites me. Of all the things I've done in my life so far, I can see myself doing this for the rest of my days, happily.

So, my biggest inspiration at the moment is my nephews company. He, his business partner, his mother, my wife, and several family friends form the company thus far. In 2017, my nephew started staffing for an insurance sales office that he worked in. His methods were really successful and word got out about what his office was achieving and he started getting approached to do the same in other offices. He started with pencil and paper for tracking things and then started using Google Sheets to track offices, emails, cadidates, Platinum Recruiter blast numbers, etc. I came on in late 2017 to help by running the Platnum Recruiter software to aggregate resumes and send out campaign blasts when there were about 50-60 offices from the East Coast to the Midwest US. I told him then that a relational DB and some sort of UI would be very necessary because the current system was not scalable as it had grown to a 10 people working on one google sheet, updating data and it was of course error ridden with no easy way to control data integrity. There was no established convention so formulas were easily wrecked by someone inputing incorrect values. Any time an office changes for example, an interviewer, the name has to be updated on two Platinum Recruiter templates, outlook bells and whistles templates, zip recruiter templates and at every stage, they are prone to errors on one or more of them as they are all done manually.

It is also very unecessarily time consuming. Each week, my wife has to set up a new sheet for everyone to work from. There are now almost 400 offices on the Weekly sheet template and only about 120-150 run on a given week, so those that are not running, the rows have to be manually hidden. I takes hours to set up each week. The schedules are submitted by the various offices via Google Forms and there is no control to make sure that they are submitting for the correct time of day, year, month. Each office goes into a their own tab on another Google sheet. Those submissions are then looked over by someone who then has to copy and paste them onto the correct row on the Weekly sheet. If a submission is incorrect, a message is sent from the person who parses the schedule tabs to another person that contacts the office to get them to correct their submission. That person contacts the office, the office replies and then the person who contacts the offices, relays back to the person handling the schedule. I made a little program that Validates a schedule to deny Monday submissions, weekend submissions, wrong week, wrong month, wrong year wrong time of day, i.e. 2 a.m. vs 2 p.m. but still have yet to build the DAO and get it to imput to a DB table.

When a new office is added, the office submits its information through yet another Google Form that goes into yet another tabbed Google Sheet. that information is used by his partner for his processes. My nephew then copies and pastes the information that he needs from that sheet to another Google sheet. It is a checklist of all of the manual taskes that he needs to accomplish to "Onboard" the office:



All of these various sheets have to be checked by my wife to make sure that the data on the weekly sheet is up to date. If not, she has to copy and paste from the different sheets to make it current. At each step, someone inevitably makes a mistake or forgets to update one of the sheets which cascades from there. I have estimated that about 40 hours a week is wated on redundant copy and pasting and parsing sheets goes on every week.

The email handlers, the family friends, respond to replies to the inital blast sent out by me from Platimun Recruiter. When they reply, they have to decide if it is a confirmation, or to provide more information to get to a confirmation. There are templates that they have to choose from in Outlook Bells and Whistles. They have to choose the correct template as well as the correct office for that template. If they choose the wrong template, which happens often, it then has to be redone and changed in SalesForce. That requires extra work and the candidate could be lost.

To invite a responder for an interview, they Go to the correct row on the Google Sheet, copy the schedule time, then go into Outlook, find the correct inbox, select reply, make sure to switch the template to the correct office, paste the appointment date into the template, then send.



Ultimately, I want to access values in Platimun Recruiter via an API of some sort, Access Outlook via an API or better yet, create a custom email program, integrated into OmniTrack to filter and track emails and the data related to them, as well as link templates. Then to provide a ui that will access a Relational DB to replace the terrible mess that is Google Sheets, Google Forms, Outlook, SalesForce, Calendly and all the other hodgepodge of cobbled together pieces that consume so many man hours.

So, I should be able to do this in say two or three months, yeah? Lol
 
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Not sure, but I don't think a spinner is the best component for this problem.



If we're talking about Swing then the usual way to choose from two (or three or some fixed number) of alternatives is a set of radio buttons.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Campbell Ritchie wrote:Not sure, but I don't think a spinner is the best component for this problem.



If we're talking about Swing then the usual way to choose from two (or three or some fixed number) of alternatives is a set of radio buttons.



I actually like the ComboBox with an enum that Campbell recommended. Now I just need to learn about enums and how to implement them in Swing.
 
Paul Clapham
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a combo box is good for that too.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I took a stab at an enum. It is showing up in my JComboBox in the dialog but the rest of the program is wrecked.


Still searching far and wide on google to learn how to make it work with my DAO and Office object.

I tried it here and eclpse isn't seeing anything wrong.



It's here where things are still unclear. the tempOffice is passed to the DAO but gender, subjectPronoun, and objectPronoun are now in the enum, not the Office object:


I don't know if I need getters and setters in the enum or... I don't know.

 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now the only hang up eclipse has is:
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't expect Office to have a JComboBox field. It should probably be using the genderComboBox to get the selected Gender enum just like the TextFields use getText() to retrieve the state but not the GUI object.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this in the DAO:


but then it wanted me to change the variables in the enum to static:

after I did I got:


Anyway, I think I need to move away from this for a few months while I further my core studies.
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's bound to be confusion because your enum is named Gender, and the enum has a field named gender which should have a corresponding getGender() method. I might have gotten a little more verbose with the naming and called the enum GenderEnum. I think this would help because now you'd have two different getters: getGenderEnum() and getGender().
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ray Gilbert wrote:I tried this in the DAO:


but then it wanted me to change the variables in the enum to static:

after I did I got:


Anyway, I think I need to move away from this for a few months while I further my core studies.

You don't want static. Each enum constant has its own copies of the fields so you have to use the Gender object followed by .getGender().
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The words “he” and “she” oughtn't to be static in the first place (I see Carey has already told you that). Please show us your Gender class.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have now found your Gender class, which looks correct, but incomplete.

Carey Brown wrote:. . . the enum has a field named gender which should have a corresponding getGender() method. . . .

I think that field is redundant because you can get the same result with a bit of jiggery‑pokery with Strings, substrings and the toString() method.

. . . Still searching far and wide on google . . .

Beware: the farther you search, the more you will find, and the more likely you are to find things weird and misleading. I can recommend somewhere, however. You will have to look a long way before you find better advice than on this link.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The words “he” and “she” oughtn't to be static in the first place (I see Carey has already told you that). Please show us your Gender class.



I took Careys tip to rename to GenderEnum.

 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never believe compiler error messages telling you to make something static.
Don't provide any setXXX() methods. Make all your fields final. The intention behind enums is that they ought to be immutable.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would simplify the toString() method myself....or something like that; there are several ways to achieve such a result. Much simpler simply to print “Male” or “Female”.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I would simplify the toString() method myself....or something like that; there are several ways to achieve such a result. Much simpler simply to print “Male” or “Female”.



Ok, I made changes to my Enum, I'm not sure what to do with the toString method yet.

This seemed to unwreck my DAO:

But my add office dialog is wrecked:


This is what I have now as far as the Enum:
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to revise your formatting Strings; that toString method will give you some very peculiar results. Apart from that, it looks good.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few hours ago, I wrote:. . . Apart from that, it looks good.

I still think the gender field is unnecessary.
 
Sheriff
Posts: 7423
505
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

A few hours ago, I wrote:. . . Apart from that, it looks good.

I still think the gender field is unnecessary.


@OP Absolutely agree with Campbell. You simply duplicating knowledge.
 
Liutauras Vilda
Sheriff
Posts: 7423
505
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Style comment. That is unconventional and noticable that you struggled to come up with parameter names, so decided to choose one letter names.

Consider having like that:

Used this keywoard tells your program that you refer to instance's variable, while the variable on the right hand side (without 'this') of assignment expression refers to constructor's parameter variable.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You need to revise your formatting Strings; that toString method will give you some very peculiar results. Apart from that, it looks good.



Do I need to %s a string when it is already established in the Enum?

And I am not having much luck understanding an enum and how to get any values from it, like I can with strings and textboxes. I tried just with a main method trying to print values.
Nothing makes sense to about it. It is the least intuitive thing I have come accross so far in Java. I can get the values but I have to state Male or Female I don't know how to implement this in the code that I have. This is my , I don't know what to do with it.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what you normally get when you try to display a String with System.out.println().

Try Arrays.toString(GenderEnum.getValues()); instead.

Rethink your decision of adding the "Enum" qualifier to the name of an enum type. That's just screaming "implementation" -- you want your names to focus on intent instead. Would you add "Class" or "Object" to every class or object you define? If not, why would "Enum" be any different? The name "Gender" is much better than "GenderEnum" in my opinion.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:That's what you normally get when you try to display a String with System.out.println().

Try Arrays.toString(GenderEnum.getValues()); instead.

Rethink your decision of adding the "Enum" qualifier to the name of an enum type.



I originally had named it simply "Gender" but at the beginning of this thread, I was advised to change the name:

Carey Brown wrote:There's bound to be confusion because your enum is named Gender, and the enum has a field named gender which should have a corresponding getGender() method. I might have gotten a little more verbose with the naming and called the enum GenderEnum. I think this would help because now you'd have two different getters: getGenderEnum() and getGender().

 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!