Need help, Getting error when converting List into String

Discussion in 'Plugin Development' started by Smalltrout, Dec 6, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Smalltrout

    Hello,

    I am having troubles converting a List into a String. The list contains players. I need to convert it into a string to message the players this list. How would i do this?

    Code:java
    1. String[] op = Operator.toArray(new String[Operator.size()]);

    This is what i am using right now to turn it into a string. When you type the command it gives you an error. All help appreciated.

    Thanks!
     
  2. Offline

    97WaterPolo

  3. Offline

    teej107

    Your current implementation is throwing errors because you are trying to convert a List of Players to an array of Strings. Strings are not Players and Players are not Strings.

    To answer your question, you will need to loop through the List and append each player's name to a String.
     
  4. Offline

    Monkey_Swag

    To Elaborate on what Teej said, you need to change your list from ArrayList<Player> to ArrayList<String> then to add a player to that list, simply do Operator.add(player.getName());
     
  5. Offline

    MoeMix

    Monkey_Swag
    He doesn't really have to. He can keep the list of players. In a for loop, loop through all the players and store the player.getName() in an ArrayList<String> defined outside the for loop so you don't keep creating lists. Then, in the for loop, add the player.getName() into the list of strings you already created.

    Now to message the players in this list just setup a for loop like this:
    Code:java
    1. for(String playerName : myListOfNames){
    2. Bukkit.getPlayer(playerName).sendMessage("wassup homie?");
    3. }
     
  6. Offline

    teej107

    No. That kind of destroys the point of my post. If your going to end up getting players from the String list anyway, just use a Player list!
    Smalltrout
    Here is some pseudo-code
    Code:
    Create StringBuilder
    for loop through Player List
    append Player name to StringBuilder
     
    After for loop, send player string from StringBuilder.
     
  7. Offline

    Smalltrout

  8. Offline

    Skionz

    Smalltrout Your using toString() on an object.
     
  9. Offline

    Smalltrout

    Im actually using toArray(). Do you know how i could stop it from displaying [ Ljava.lang.String;@2883a695. It is properly adding the player to the list its just using that. Skionz
     
  10. Smalltrout
    Use Player.getName() instead of Player
     
  11. Offline

    teej107

    Post your code so I can help you learn from your mistake.
     
  12. Offline

    Smalltrout

    teej107
    Code:java
    1. private List<String> Operator = new ArrayList<String>();
    2.  
    3.  
    4.  
    5. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    6. Player player = (Player) sender;
    7. String[] op = Operator.toArray(new String[Operator.size()]);
    8. if(commandLabel.equalsIgnoreCase("report")){
    9. if (Operator.isEmpty()){
    10. player.sendMessage(ChatColor.DARK_RED + "Sorry, There are no Operators online right now");
    11. }else{
    12. player.sendMessage(ChatColor.RED + "Current Operators online right now: " + op );
    13.  
    14. }
    15.  
    16.  
    17. }
    18.  
    19. return false;
    20.  
    21. }
    22.  
    23. @EventHandler
    24. public void onPlayerJoin(PlayerJoinEvent event){
    25. Player player = event.getPlayer();
    26. if(player.isOp()){
    27. Operator.add(player.getName());
    28. player.sendMessage(ChatColor.GREEN + "Thank you " + ChatColor.RED + player.getName() + ChatColor.GREEN + " for joining " + player.getServer().getName());
    29. }
    30. }
    31.  
    32. @EventHandler
    33. public void onPlayerQuit(PlayerQuitEvent event){
    34. Player player = event.getPlayer();
    35. if(player.isOp()){
    36. Operator.remove(player.getName());
    37. }
    38. }
     
  13. Offline

    mythbusterma

    Smalltrout

    Your code is quite.......unnecessary. You can just do Sever#getOperators().

    So why do you feel the need to copy them to an array in the first place? There is absolutely no reason to do so.
     
  14. Offline

    teej107

    Smalltrout
    *hint* you don't need to convert the list to an array
     
  15. Offline

    Smalltrout

    Wow... didn't know about that. Thanks a lot! haha mythbusterma
     
Thread Status:
Not open for further replies.

Share This Page