True or False in an array

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;
 }

Answer

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

Leave a Reply

Your email address will not be published. Required fields are marked *