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
I also need to rethink the "gender" field. It has been a paradigm carried over from my attempts to do this with Strings. Campbell has been trying to tell me.

 
Sheriff
Posts: 7423
505
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


The advise seems to be given based on your code design, so it wouldn't cause confusion of some sort. However, as been mentioned, why do you think you need "gender" field as well as corresponding getGender().

In the UI part you can decide how you display when you face an object of type Gender.MALE for instance. Imagine you'd have an app which can be displayed in the language of your choice. Probably you wouldnt' go and wouldn't re-implement what data gets stored, but rather you'd change the way data gets displayed in EN, LT, RU languages based on the Gender type.

EN view:
String gender = gender == Gender.MALE ? "male" : "female"

LT view
String gender = gender == Gender.MALE ? "vyras" : "moteris"

RU view
String gender = gender == Gender.MALE ? "человек" : "женщина"
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

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


The advise seems to be given based on your code design, so it wouldn't cause confusion of some sort. However, as been mentioned, why do you think you need "gender" field as well as corresponding getGender().

In the UI part you can decide how you display when you face an object of type Gender.MALE for instance. Imagine you'd have an app which can be displayed in the language of your choice. Probably you wouldnt' go and wouldn't re-implement what data gets stored, but rather you'd change the way data gets displayed in EN, LT, RU languages based on the Gender type.

EN view:
String gender = gender == Gender.MALE ? "male" : "female"

LT view
String gender = gender == Gender.MALE ? "vyras" : "moteris"

RU view
String gender = gender == Gender.MALE ? "человек" : "женщина"



When I started, I had a textField in AddOffice.java that I was typing in male/female from which it would change the values of subjectPronoun and objectPronoun accordingly. The subpro and obpro are then used in Templates.java to change those pronouns in an email correspondence template. I was having difficulty implementing it and I started this thread with the question of what I was doing wrong. I was were my mistake was and the correction fixed it. It changed the template values when I typed Male and Female into the textField. Campbell then pointed out, that though that worked, it was a bad concept because you can type anything into a textField. He suggested an Enum with a comboBox in the dialog. I love the idea and I know it will be quite an advancement for me and my code but it is baffling me as to how to implement it. I'm just breaking the crap out of my code, hacking at it.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Her is what I have now, with suggestions so far.
 
Sheriff
Posts: 14764
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
ignore(That only highlights the point mentioned before by others that it doesn't make sense for a Gender enum to have a gender field. It's redundant. Would you have a Color enum have a color field?)

-- I missed all the other messages posted relevant to this.
 
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
Looks reasonable to me (ignoring the fact that formatting could be improved).
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The toString() isn't quite right. You're passing in a third parameter whose value will not be displayed given the format string you're using.
 
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
The first (only) %s is paired up with subjectPronoun. You don't have an additional %s to be paired up with objectPronoun. In your format the number and order of % specifications need to match the number and order of parameters that follow.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Looks reasonable to me (ignoring the fact that formatting could be improved).



I really don't fully understand the formatting actually. %s toLowerCase and %S is toUpperCase. The variables are already in lower case, it seems redundant.

and while

 
Junilu Lacar
Sheriff
Posts: 14764
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
Also, consider this: you have he/she and him/her as attributes of the MALE and FEMALE enum values. What happens when someone asks for the possessive his/hers?

You'll probably think to add another field and corresponding getters and constructor parameters. However trivial those changes may seem, they still hint at a violation of a basic design principle: the Open-Closed Principle. Not suggesting the current design won't work or that it's wrong, just saying that it might be a good exercise to see what can be done to make accommodating the addition of other pronoun forms a little easier.

This is a little more advanced though so feel free to come back to it when you think you're ready to take on the design challenge.
 
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
Gender.values() return T[] - an array of your all defined instances in a Gender enum. And arrays don't override toString() so you get what you get as a displayed value.

try:
System.out.println(Arrays.toString(Gender.values()));
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jshell> enum Gender {MALE, FEMALE}
|  created enum Gender

jshell> Arrays.toString(Gender.values())
$3 ==> "[MALE, FEMALE]"
 
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

Ray Gilbert wrote:%s toLowerCase and %S is toUpperCase. The variables are already in lower case, it seems redundant.


Not quite. %s is not lower-case, it is whatever case the String is stored in. %S on the other hand, is going to coerce all the letters in the String to upper-case.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Gender.values() return T[] - an array of your all defined instances in a Gender enum. And arrays don't override toString() so you get what you get as a displayed value.

try:
System.out.println(Arrays.toString(Gender.values()));



Yep. that did it.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:The first (only) %s is paired up with subjectPronoun. You don't have an additional %s to be paired up with objectPronoun. In your format the number and order of % specifications need to match the number and order of parameters that follow.



Thank you Carey

 
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 combo box no longer displays "MALE" or "FEMALE". it displays:



 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, found the problem:

I took it out and:

 
Junilu Lacar
Sheriff
Posts: 14764
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
If you didn't override the toString() method, it would display MALE and FEMALE. An enum by default has a name() method that would return the name of the enum value as it is defined in the source code. By default, toString() will return the value returned by name() but you have an overridden toString() so that's what you're seeing.

See this: https://readlearncode.com/java-6/enum-how-to-use-name-and-tostring-methods-correctly/
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having yet to deal with toString, toString.format, and especially Enums, I was not clear what it was doing. I'm just trying to get the values of subjectPronoun and objectPronoun from the Enum in the JComboBox to the Office object to then pass to the OfficeDAO. I still don't any clue how to do that.
 
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

Ray Gilbert wrote:Having yet to deal with toString, toString.format, and especially Enums, I was not clear what it was doing. I'm just trying to get the values of subjectPronoun and objectPronoun from the Enum in the JComboBox to the Office object to then pass to the OfficeDAO. I still don't any clue how to do that.


The Office object should not be directly dealing with subjectPronoun and objectProun, it should only have the GenderEnum. If subsequent steps requires access to the xpronouns then they should use getters from the GenderEnum.
 
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay then. How about:



Or maybe "name" instead of "description"?
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, everything is working but the input to the database subpron and objpron are inputting

"javax.swing.JComboBox[,102,124,190x25,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=FEMALE]"

from this: I guess this is where the toString Override comes back to bite me in the butt. How do I both show MALE/FEMALE in the combo but get string values for subpron and objpron?
 
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
You're not forced to use toString(), you can make your own getPronouns() method that returns the string you want.
 
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
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You're not forced to use toString(), you can make your own getPronouns() method that returns the string you want.



I have:

And I tried that:

But I get: "The method getSubjectPronoun() is undefined for the type JComboBox<Gender>"
 
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

Carey Brown wrote:

 
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
A ComboBox has all possible Gender enum choices, you first have to get the selected item from the ComboBox.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:A ComboBox has all possible Gender enum choices, you first have to get the selected item from the ComboBox.



This is what I ended up with:
Watch Here:
 
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
I would have put the cast here instead
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I would have put the cast here instead



I still have so much to learn. I fixed the video link in the last post btw.
 
Paul Clapham
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I would have put the cast here instead...



A cast? Why isn't it a JComboBox<Gender> so you don't need a cast?
 
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
Doc says getSelectedItem() returns Object. It doesn't indicate that it returns the generic type. Am I missing something?
 
Paul Clapham
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of us are missing something, namely why it returns Object instead of E. I sure don't know why they did that. So how about:

 
Paul Clapham
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I sure don't know why they did that.



Actually I do know, after reading the docs some more.

The API wrote:If the combo box is editable, then this value may not have been added to the combo box with addItem, insertItemAt or the data constructors.

 
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

At risk of causing more confusion, I think it would be better to retain the class name Gender and get rid of the gender field because the latter is simply duplication.
How do you get information out of an enum constant? Same way you get information out of any object: with its getXXX methods. You already have two getXXXPronoun() methods, which you can use. You also need some additional information, which is in this JLS (=Java® Language Specification) link.
  • 1: Your enum implicitly extends Enum<Gender>. Please look through the Enum class and you will find it has methods like name(). You can use that to get “Male” or “Female” as I showed you earlier.
  • 2: An enum constant is a full‑blown object, so you can write methods, as you have been doing already. There are alternative ways to get “him” out of MALE; the JLS link shows you how you can write an abstract method and override it. You could write @Override public String getObPronoun() { return "him"; }. Then you wouldn't need the field or the constructor parameter.
  • That JLS section is full of useful examples showing how you can use enums.
    Consider writing yourself a utility class with a toTitleCase(String) method. Title case means you have the first letter Capitalised and other letters lower‑case. It is so called because that is how book titles are usually printed, “The Call Of The Wild”. Case changes don't work very well for a few letters, e.g. the German ß.

    Sorry but I seem to have given you the wrong Java™ Tutorials link earlier: this page will be more helpful. This old link included a sentence something like this:-

    Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers.

    The first edition of the Java™ Tutorials contained a similar sentence. Maybe somebody complained because the bit about “glorified integers” disappeared from later editions. It also had PLUTO in the Planet enum in those days.


    [edit] 3 spelling corrections an→can ontained→contained and Overrride→Override
     
    Campbell Ritchie
    Marshal
    Posts: 67533
    257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:. . . Am I missing something?

    Whenever I go near things like combo box, I start worrying that Swing® needs to be dragged kicking and screaming into the 21st century. Some classes have constructors taking Vector as parameter, not List. Some classes are parametrised and some not.
     
    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
    [Edit: I didn't realize there was a whole second page of posts, so this post may be redundant.]

    Ray Gilbert wrote:This is my , I don't know what to do with it.


    The docs may be overwhelming, but very basically "%s" is a marker that will be substituted for a string that you supply.  So you will need one marker for each of the arguments after the format string.
    This will become "Male He Him" if you send it those arguments.
     
    Campbell Ritchie
    Marshal
    Posts: 67533
    257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:. . . The docs may be overwhelming . . .

    Maybe these two Java™ Tutorials sections will be easier to understand: 1 2. I think there may be a third section too: go here and search for “formatting”.
     
    Ray Gilbert
    Ranch Hand
    Posts: 109
    1
    MyEclipse IDE MySQL Database Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote: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.



    I get that and have made an attempt but then it wrecks everything else.

    The problem is that my DAO uses

    if I take out

    then I have to have a tempAddress. You can't put both in the Method:

    So now I have no way to access street, city, state, and zip.

     
    Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
    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!