Click here to Skip to main content
15,905,558 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here are the two codes:
1.Avenger.java
Java
package com.avengers;

public class Avenger {

     String[] name = {"Iron Man","Captain America","Thor","Black Widow","Hulk"};
     String[] weapon = {"Repulsor","Shield","Stormbreaker","Baton","Mjolnir"};
     String[] planet = {"Titan","Earth","Asgard","Vormir","Sakaar"};
     int[] age = {48,27,1500,34,58};
     int[] power = {9,10,20,6,8};
     String names,weapons,planets;
     int ages,powers;

    public void getDetails() {
        for(int i=0;i<5;i++){
            names = name[i];
            ages = age[i];
            powers = power[i];
            weapons = weapon[i];
            planets = planet[i];
        }
    }

    public void displayDetails() {
            System.out.println("Name: " + names);
            System.out.println("Age: " + ages);
            System.out.println("Power: " + powers);
            System.out.println("Weapon: " + weapons);
            System.out.println("Planet: " + planets);
        }
}


2.Main.java
Java
package com.avengers;

public class Main {

    public static void main(String[] args) {

        Avenger[] avenger=new Avenger[5];
        for(int i=0;i<5;i++)
        {
            avenger[i]=new Avenger();
            avenger[i].getDetails();
            System.out.println("Data at index "+i);
            avenger[i].displayDetails();
        }

    }
}


What I have tried:

I tried to print all the properties of avenger object but it prints only values at last index of the array i.e. Hulk data.
Posted
Updated 15-Oct-20 21:01pm
v2

That's because your GetDetails in the main is being called within a for loop, and the GetDetails function already has another for loop.

So the return value that you get is the last one.

Change your GetDetails function to return only one position of the array and give that position as a parameter


EDIT: Additionally, just a thought.
I would change the way you name things.
In my case I use the "names" for the array and the "name" as a single value.

I mean:
Java
String[] names = {"Iron Man","Captain America","Thor","Black Widow","Hulk"};
...
...
...
name = names[i]
 
Share this answer
 
v2
I suppose you should end up with an array of Avengers.
Try, for instance:
Java
public class Avenger
{
  String name , weapon,planet;
  int age , power;

  public Avenger(String name, String weapon, String planet,int  age, int power )
  {
    this.name = name;
    this.weapon = weapon;
    this.planet = planet;
    this. age = age;
    this.power = power;
  }

  public void displayDetails()
  {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Power: " + power);
    System.out.println("Weapon: " + weapon);
    System.out.println("Planet: " + planet);
  }

  public static void main( String arg[])
  {
    Avenger [] avenger = AvengerFactory.getAvengers();
    for (Avenger a : avenger)
    {
      a.displayDetails();
      System.out.println();
    }
  }
}

class AvengerFactory
{

  final static int Avengers = 5;
  final static String[] name = {"Iron Man","Captain America","Thor","Black Widow","Hulk"};
  final static String[] weapon = {"Repulsor","Shield","Stormbreaker","Baton","Mjolnir"};
  final static String[] planet = {"Titan","Earth","Asgard","Vormir","Sakaar"};
  final static int[] age = {48,27,1500,34,58};
  final static int[] power = {9,10,20,6,8};

  static Avenger [] s_avenger = null;

  public static Avenger[] getAvengers()
  {
    if ( s_avenger == null )
      createAvengers();
    return s_avenger;
  }

  static void createAvengers()
  {
    s_avenger = new Avenger[Avengers];
    for (int i=0; i<Avengers; ++i)
    {
      s_avenger[i] = new Avenger(name[i], weapon[i], planet[i], age[i], power[i]);
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900