0

Disclaimer I'm new to Java still learning. I have created a class called CharacterProfiles as part of my textgame to store information about a certain characters. I have created two characters so far (skeleton and zombie) and am trying to put them into an array list so that a random generator can pick either skeleton or zombie everytime the game runs. I'm having trouble getting the computer to pick one of the two characters from inside the Arraylist and print it.

My code so far.

public static void levelOne {

CharacterProfiles skeleton = new CharacterProfiles("Sword", 50, 100, 2, 100);

 CharacterProfiles zombie = new CharacterProfiles("Sword", 100, 100, 2, 120);

        List<CharacterProfiles> enemies = new ArrayList<>();
        enemies.add(skeleton);
        enemies.add(zombie);

        String randomEnemyGenerator = enemies.get(new Random().nextInt(enemies.size()));
        System.out.println(randomEnemyGenerator);

}

The: String randomEnemyGenerator = enemies.get(new Random().nextInt(enemies.size())); I have just gotten from trying to look up solutions on stackOverflow (I tried some other ones I found as well) but nothing seems to work. The error code I get is: Incompatible types Required java.lang.String found Characterprofiles.

Any solutions would be appreciated, thanks!

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
G.Bar
  • 21
  • 4
  • 1
    You store elements of class `CharacterProfiles` in your `ArrayList`, and when you want to get one of them in `randomEnemyGenerator` you declare it as a `String`. That's basic Java types, you should read the docs. It should be `CharacterProfiles randomEnemyGenerator = ...` – Kaddath Mar 13 '19 at 10:09
  • I changed it from String to Characterprofiles which took away the error, thankyou. When I ran it though, it printed CharacterProfiles@59e84876 instead of skeleton or zombie. Any idea what that means? – G.Bar Mar 13 '19 at 10:12
  • 1
    `println` on a class object will automatically call its `toString` method because it expects a `String`, if there is none, the default Object `toString` ouputs what you get. see [this question](https://stackoverflow.com/q/27647567/7393478) – Kaddath Mar 13 '19 at 10:16

0 Answers0