0

Well I'm a beginner and I would like to know how to display the array I made(It's an Integer array). I'm just trying to create a simple calculation program that accepts user input on which fighter they would like to pick, and how much money they would like to bet. Again, I'm just a beginner so no hate please.

public class Fighter {
private int health;
private int attack;
private int money;

int[] fighter={health,attack,money};

public Fighter(int h, int a, int m){

    health=h;
    attack=a;
    money=m;

}

public int[] getFighter(){

        return fighter;
}

}

*******END OF FIGHTER CLASS*****

import java.util.*;
public class FighterCreator {

public static void main(String[] args){

    Fighter newFighter=new Fighter((int)(Math.random() * 701),
                                   (int)(Math.random() * 39), 7000);
    System.out.print(newFighter.getFighter());
}
}

When I run the code it just displays hexadecimal, can you guys please explain it in a way a beginner would understand, thanks.

Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56

1 Answers1

4

Since each object has toString() method, the default is displaying the class name representation, then adding @ sign and then the hashcode of the array instance.

To view the elements inside the array, you can use Arrays#toString:

System.out.println(Arrays.toString(yourArray));

(See its implementation for details to better understand what's going on)


TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Maroun
  • 94,125
  • 30
  • 188
  • 241