Solved Splitting int from currency on sign

Discussion in 'Plugin Development' started by NullPointerExptn, Jun 17, 2018.

Thread Status:
Not open for further replies.
  1. Right so I basically want to charge a player the amount that is displayed on the sign. How would I do this? The sign reads £x (x being any value given). How would I seperate the currency sign from the integer then change the String (which is what the value is) into an integer? I've tried parsing the int but it comes back as a numberformatexception.

    This is what the sign looks like:

    Code:
     
    event.setLine(2, ChatColor.GOLD + "Price " + ChatColor.GRAY + ": " + ChatColor.GREEN + "£" + ChatColor.GREEN + event.getLine(2));
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @NullPointerExptn Strip the color.
    Split on £, parse the second part as double.
     
  3. I'm probably being really stupid, this is what I've done and it still isn't working.

    Code:
    String line2 = s.getLine(2);
    String[] line_2 = line2.split("\\£");
    String line = line_2[1];
    double line1 = Double.parseDouble(line);
    
     
  4. Offline

    KarimAKL

    @NullPointerExptn This worked for me:
    Code:Java
    1.  
    2. if (sign.getLine(2).contains("£")) {
    3. try {
    4. String getAmount = sign.getLine(2).substring(1);
    5. Integer.parseInt(getAmount);
    6. Integer amount = Integer.valueOf(getAmount);
    7. //Remove amount
    8. } catch (NumberFormatException e) {
    9. //Send some message?
    10. }
    11. }
    12.  

    You can just edit some stuff in this to make it work for you, hope this helps.
     
  5. Offline

    timtower Administrator Administrator Moderator

  6. I put this here:
    Code:
    if (s.getLine(1).equalsIgnoreCase(ChatColor.GOLD + "Tier " + ChatColor.GRAY + ": " + ChatColor.GREEN + "1")) {
    
    if (s.getLine(2).contains("£")) {
    try {
    String getAmount = ChatColor.stripColor(s.getLine(2).substring(1));
    Integer.parseInt(getAmount)
    ;Integer amount =Integer.valueOf(getAmount);
    e.getPlayer().sendMessage("TEST " + amount);
    } catch (NumberFormatException exptn) {
    exptn.printStackTrace();}
    }
    
    But it throws this:

    Code:
    java.lang.NumberFormatException: For input string: "aú1"
    [18:52:51 WARN]:  at java.lang.NumberFormatException.forInputString(Unknown Source)
    [18:52:51 WARN]:  at java.lang.Integer.parseInt(Unknown Source)
    [18:52:51 WARN]:  at java.lang.Integer.parseInt(Unknown Source)
    [18:52:51 WARN]:  at me.admin.rentahorse.listeners.SignClick.onSignClick(SignClick.java:44)
    
    This is line 44
    Code:
    Integer.parseInt(getAmount);
    
     
  7. Offline

    KarimAKL

  8. Offline

    timtower Administrator Administrator Moderator

    @NullPointerExptn Why do you substring the line? Just us the entire line.
    And you need to split it on the £
     
  9. This is the sign. I've tried splitting it on the £, it doesn't work
     

    Attached Files:

  10. Offline

    timtower Administrator Administrator Moderator

    @NullPointerExptn My explanation was based on the code in the first line. With the Price: added.
    Not without it.
     
Thread Status:
Not open for further replies.

Share This Page