Solved Splitting isn't working.....

Discussion in 'Plugin Development' started by Datdenkikniet, Feb 14, 2014.

Thread Status:
Not open for further replies.
  1. So, I am making a plugin which displays some numbers, but those have to be rounded a bit.
    My problem is an ArrayIndexOutOfBoundsException, because this line:
    Code:java
    1. String[] retunsArray = returns.split(".");

    of code in this thing (on line 28 and 51):
    full class (open)
    Code:java
    1. package me.datdenkikniet;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.scheduler.BukkitRunnable;
    6.  
    7. public class Runnable extends BukkitRunnable{
    8. Main plugin;
    9. public Runnable(Main instance){
    10. plugin=instance;
    11. }
    12. @Override
    13. public void run() {
    14. for (Player p: plugin.getServer().getOnlinePlayers()){
    15. HeadsUpDisplay.displayTextBar(ChatColor.GOLD + "Damage: " +ChatColor.GRAY + getExtraDamage(p) + "%" + ChatColor.GOLD + " Resistance: " +ChatColor.GRAY + getResistance(p) + "%", p);
    16. }
    17. }
    18. private String getExtraDamage(Player p){
    19. String returns = null;
    20. Double damage = plugin.getConfig().getDouble("extra damage");
    21. if (p.getLevel() > 0){
    22. returns = String.valueOf(damage*p.getLevel() + 100);
    23. }
    24. p.sendMessage(returns);
    25. if (returns == null){
    26. returns = "0.0";
    27. }
    28. String[] retunsArray = returns.split(".");
    29. if (!(retunsArray[1].length() < 2)){
    30. returns = returns.substring(0,2);
    31. }
    32. p.sendMessage(retunsArray[0] + retunsArray[1]);
    33. return returns;
    34. }
    35. private String getResistance(Player p) {
    36. String returns = null;
    37. Double resist = plugin.getConfig().getDouble("extra resistance");
    38. Double maxResist = plugin.getConfig().getDouble("max resistance");
    39. if (p.getLevel() > 0){
    40. if (!(resist * p.getLevel() > maxResist)){
    41. returns = String.valueOf(100 - ((1-(resist/100 * p.getLevel()))*100));
    42. } else {
    43. returns = String.valueOf(100 - ((1 - maxResist/100)*100));
    44. }
    45. }
    46. if (returns == null){
    47. returns = "0.0";
    48. }
    49. String[] returnsArray = returns.split(".");
    50. if (!(returnsArray[1].length() < 2)){
    51. returns = returns.substring(0, 2);
    52. }
    53. return returns;
    54. }
    55. }
    56.  

    isn't doing what its supposed to be (split the string :))
     
  2. Offline

    adam753

    Short answer
    Do this instead:
    Code:
    String[] retunsArray = returns.split("\\.");
    Long answer
    The string split function doesn't necessarily take one character, it actually takes a regular expression. "Regex" is a sort of markup language for strings, you can google it if you don't know what it is. The problem you have is that a period in regex means "any character", so what you're doing now is splitting the string on every character in it and resulting in a ton of blank strings. To get around this you need to escape it by putting \\ before the period, so: "\\."
     
  3. adam753 okay, thank you very much, I always thought a "Regex" was kind of a string thing (and it actually is, but differently from what I thought)
     
Thread Status:
Not open for further replies.

Share This Page