I have 3 arrays with several informations.
String[] personages = {"Tya", "Milo", "Lili"};
int[] ages = {21,30,15};
int[] sex = {1,2,1}; // 1 = Woman & 2 - Man
When, I want to display each information, I would like to display in the sex
array the value true
or false
.
For example the value 1 represents a woman
and the value 2 represents a man
.
I would like to get this final result:
Personage 1 : Name Tya | Age : 21 | Sex : False
Personage 2: Name Milo | Age : 30 | Sex : True
Personage 3: Name Lili | Age : 15 | Sex : False
I have as error message => "error: not a statement true;"
for(int i=0; i<personages.length; i++){
if(sex[i] == 1){
true;
}
System.out.println("Personage " + (i+1) + " : " + personages[i] + " | age : " + ages[i] + " | sex : " + sex[i]);
}
I think my condition is not good?
if(sex[i] == 1){
true;
}
You need to declare the variable that will contain the boolean.
bool isMale = sex[i] == 1;
And then
System.out.println("Personage " + (i+1) + " : " + personages[i] + " | age : " + ages[i] + " | sex : " + isMale);
Just replace the array of it by an array of booleans:
boolean[] sex = {false,true,false};
and print:
for(int i=0; i<personages.length; i++){
System.out.println("Personage " + (i+1) + " : " + personages[i] + " | age : " + ages[i] + " | sex : " + sex[i]);
}
Better would be to redesign your program completely to use a class to represent the Person instead of an array:
public class Person {
String name;
int age;
Gender Gender;
}
and an Enum for the Genders:
public enum Gender {
MALE,
FEMALE;
}
and then a List of those Object:
List<Person> personages = new ArrayList<>();
A Running example:
public class Person {
final String name;
final int age;
final Gender gender;
public Person(String name, int age, Others_git.Gender gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
@Override
public String toString() {
return "Name "+this.name+ "| Age : "+this.age + " | Sex : "+this.gender;
}
}
public class Main {
public static void main(String[] args) {
List<Person> personages = new ArrayList<>();
personages.add(new Person("Tya", 21, Gender.FEMALE));
personages.add(new Person("Milo", 21, Gender.MALE));
personages.add(new Person("Lili", 21, Gender.FEMALE));
for(int i=0; i<personages.size(); i++){
System.out.println("Personage " + (i+1) + " : " + personages.get(i));
}
}
}
Output:
Personage 1 : Name Tya| Age : 21 | Sex : FEMALE
Personage 2 : Name Milo| Age : 21 | Sex : MALE
Personage 3 : Name Lili| Age : 21 | Sex : FEMALE
Go with this:
for(int i=0; i<personages.length; i++){
Boolean sexField = sex[i] == 1;
System.out.println("Personage " + (i+1) + " : " + personages[i] + " | age : " +
ages[i] + " | sex : " + sexField);
}
The answer to your question would be to use an advanced Enum (please feel free to replace this with the proper term)
public enum Sex {
MALE(false), FEMALE(true);
boolean sexAsBoolean;
public Sex(boolean flag) {
this.sexAsBoolean = flag;
}
@Override
public String toString() {
return String.valueOf(flag);
}
}
What this does is construct an enum which can be addressed via either Sex.MALE
or Sex.FEMALE
, that has an inner variable sexAsBoolean
which is set to the argument of the enum - so for MALE sexAsBoolean == false
and FEMALE sexAsBoolean == true
.
As an important side note:
The following is a tell tale sign that you're actually doing something "wrong" - gathering values from which you know they belong together without putting them together - esp. if personages[0] belongs to ages[0] and sex[0].
String[] personages = {"Tya", "Milo", "Lili"};
int[] ages = {21,30,15};
int[] sex = {1,2,1}; // 1 = Woman & 2 - Man
Then what you really want is:
public class Personage {
String name;
int age;
Sex sex; // This is the enum we wrote above.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.