0

I am trying to find a better way to write the following code:

public static void initTab(Player player) {
    player.getPacketSender().sendString(15002, "Achievements - " + amountCompleted(player, Difficulty.TOTAL) + "/30");
    player.getPacketSender().sendString(15007, "Easy Tasks - " + amountCompleted(player, Difficulty.EASY) + "/10");
    player.getPacketSender().sendString(15008, getColor(player, 0, 0)+Achievements.Tasks.TASK1.getName());
    player.getPacketSender().sendString(15009, getColor(player, 1, 0)+Achievements.Tasks.TASK2.getName());
    player.getPacketSender().sendString(15010, getColor(player, 2, 0)+Achievements.Tasks.TASK3.getName());
    player.getPacketSender().sendString(15011, getColor(player, 3, 0)+Achievements.Tasks.TASK4.getName());
    player.getPacketSender().sendString(15012, getColor(player, 4, 0)+Achievements.Tasks.TASK5.getName());
    player.getPacketSender().sendString(15013, getColor(player, 5, 0)+Achievements.Tasks.TASK6.getName());
    player.getPacketSender().sendString(15014, getColor(player, 6, 0)+Achievements.Tasks.TASK7.getName());
    player.getPacketSender().sendString(15015, getColor(player, 7, 0)+Achievements.Tasks.TASK8.getName());
    player.getPacketSender().sendString(15016, getColor(player, 8, 0)+Achievements.Tasks.TASK9.getName());
    player.getPacketSender().sendString(15017, getColor(player, 9, 0)+Achievements.Tasks.TASK10.getName());
}

I was thinking about using a for loop to loop through the ID's (15008 , 15009, and so on... ) but I don't know how I would loop through the enum values in the same way.

I am talking about this: Achievements.Tasks.TASK1.getName()

I have an enum called Tasks and here is an example of some values that are in it:

    TASK1(Difficulty.EASY, TaskType.GLOBAL, "Easy Test1", "Test Description.", 1, new int[]{8147, 8148}),
    TASK2(Difficulty.EASY, TaskType.GLOBAL, "Easy Test2", "Test Description.", 1, new int[]{8150, 8151}),
    TASK3(Difficulty.EASY, TaskType.GLOBAL, "Easy Test3", "Test Description.", 1, new int[]{8153, 8154}),
    TASK4(Difficulty.EASY, TaskType.GLOBAL, "Easy Test4", "Test Description.", 1, new int[]{8156, 8157}),
    TASK5(Difficulty.EASY, TaskType.GLOBAL, "Easy Test5", "Test Description.", 1, new int[]{8159, 8160}),

getName() gets the first string in the values: "Easy Test1"

What I would like to know: An easy way to loop through the enum values so I can use the getName() method for all of the values in only a few lines of code.

Summary

I need to loop through this statement:

player.getPacketSender().sendString(15008, getColor(player, 0, 0)+Achievements.Tasks.TASK1.getName());

What I need to know how to do is loop through this:

Achievements.Tasks.TASK#.getName()

This question is not a duplicate because I am asking to loop through two different things. I need to loop through the enum and an int (15008, 15009, etc..) at the same time. Not just the enum, and not just the int. Can I nest the for loop for enums inside of the for loop for ints?

Charlie
  • 90
  • 9
  • This seems to be a common use case, and the enum class probably has a method to return all enums :) – ZhongYu May 30 '15 at 03:25
  • I'd just obtain all the enum values with the #values() method and then iterate over that array. Access the current element with your index of the loop (e. g. 'i') and also use it for the other number by adding sth. It's not elegant, but it works. – KnorpelSenf May 30 '15 at 06:25

1 Answers1

3

See Oracle Documentation: All the constants of an enum type can be obtained by calling the implicit public static T[] values() method of that type. If that's what you want, you can do something like this:

class EnumLoop {
  enum Color {RED, BLUE, GREEN};
  public static void main(String[] args) { 
    for (Color c : Color.values()) {
      System.out.println(c.name());
    }
  }
}

Here is the Demo.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
  • I think you may have misunderstood. I need to repeat this line: `player.getPacketSender().sendString(15017, getColor(player, 9, 0)+Achievements.Tasks.TASK10.getName());` What I am asking is how to loop through the enum values like: `Achievements.Tasks.TASK#.getName())` – Charlie May 30 '15 at 03:29
  • Are you saying your enum has a `getName()` method? Like [this](http://ideone.com/fwLWDq)? – Kedar Mhaswade May 30 '15 at 03:39
  • The get name method is irrelevant I just need to know how to loop through the name of the value like TASK1, TASK2, TASK3. Instead of calling getName over and over again – Charlie May 30 '15 at 03:41