The program records employee data. I'm interested in the Gender variable. I want to use a boolean for this variable. So I designate female / male gender:

  public void setGender(boolean gender) { this.gender = gender; if(gender == true){ System.out.println("Male"); }else{ System.out.println("Female"); } } 

Next, in my other class there is a multidimensial array , which takes data using String

 String[][] records = {{"123456U", "Maria", "Larionova", "male", "Administration", "50", "1"}, {"578943I", "Lora", "Tamm", "female", "female", "100", "2"}, {"124783H", "Victor", "Rink", "male", "Transport", "150", "1"} }; public EmployeeMenu() { super("Personnel Application"); for (int i = 0; i < records.length; i++) { list.add(new Employee(records[i][0], records[i][1], records[i][2], Boolean.parseBoolean(records[i][3]), records[i][4], Integer.parseInt(records[i][5]), Integer.parseInt(records[i][6]))); } 

And naturally, when I display the melons in the window, in the place of gender I have written false/true instead of male/female , although it is exactly male/female that is necessary!

I call this method using Swing components:

 genderLabel = new JLabel("Gender: "); genderTextField = new JTextField(10); genderTextField.setEditable(false); 

Thank you in advance!

  • You probably confused toString() and System.out.println() - Leonid Lunin
  • Show the code where you display the data in the window. - Arsenicum
  • @Leonid Lunin, you may be right. I have only one separate toString method that is created to display data. public String toString() { return "PPS Number: " + pps + "\nLast Name: " + surname + "\nFirst Name: " + name + "\nGender: " + gender + "\nDepartment: " + dep + "\nSalary: " + salary + "\nFulltime: " + fulltime; } public String toString() { return "PPS Number: " + pps + "\nLast Name: " + surname + "\nFirst Name: " + name + "\nGender: " + gender + "\nDepartment: " + dep + "\nSalary: " + salary + "\nFulltime: " + fulltime; } - Alex
  • @Alex, please: genderLabel = new JLabel("Gender: "); genderTextField = new JTextField(10); genderTextField.setEditable(false); genderLabel = new JLabel("Gender: "); genderTextField = new JTextField(10); genderTextField.setEditable(false); - Alex

1 answer 1

  public String getGender() { return gender?"Male":"Female"; } 

 public String toString() { return "PPS Number: " + pps + "\nLast Name: " + surname + "\nFirst Name: " + name + "\nGender: " + getGender() + "\nDepartment: " + dep + "\nSalary: " + salary + "\nFulltime: " + fulltime; } 

I will assume that the contents of the list element are responsible for the final output

 list.add(new Employee(records[i][0], records[i][1], records[i][2], Boolean.parseBoolean(records[i][3]), records[i][4], Integer.parseInt(records[i][5]), Integer.parseInt(records[i][6]))); 

In this case, the error is that you are using Boolean.parseBoolean(records[i][3]) trying to parse a string of the form "male" / "female", whereas this method should parse "false" / "true". In general, you have some sort of confusion. It is not clear where it is boolean , and where it turns into a string. Try storing it in boolean , working with it only as with boolean , and only when displaying it, turn it into a string using this.gender?"male":"female" method. And absolutely no need to store / set it in the form of "male" / "female" or (God forbid) "true" / "false", turning it into a boolean when reading: the whole meaning of the conversion to a boolean type is lost, it turns out that it is set / is stored as a string, then for some time turns into a boolean, and when output, then turns into a string again.

  • I added this method. Now I have "Female" displayed everywhere in the window, regardless of what the "Male" should also be displayed - Alex
  • So this.gender is false everywhere. Show the code where you added it (the class as a whole), and most importantly, how and from where you call it, then I can tell where the error is. UPD. I saw your comment on the question, corrected the answer. - Pavel Krizhanovskiy
  • thanks, so far all the same problem. I also updated my question, and added a piece where I call this method. - Alex
  • It is very difficult to understand, by fragments. It looks like fortune telling on the coffee grounds. I will make one more assumption, I will add to the answer now. - Pavel Krizhanovskiy
  • Thank you so much for the detailed explanation! I added to your corrections: String[][] records = {{"123456U", "Maria", "Larionova", "true", "Administration", "50", "1"}}; so that the value in the string is not Male , but true , and now everything works! - Alex