Solved Changing String to int

Discussion in 'Plugin Development' started by ThatGuyWhoDied13, Jan 12, 2014.

Thread Status:
Not open for further replies.
  1. Hey, I have a method t create dyed armor.

    Now here's the problem: I want it t be used via command I have everything set up so the command is "/armor <armor piece> <r> <g> <b>" but I can't do this because in the armor method I'm using it needs ints not Strings.

    So how do I convert Strings to ints??

    Thanks in advanced
     
  2. Offline

    Scullyking

    Code:java
    1. int intHere = Integer.parseInt(stringHere);

    Or
    Code:java
    1. Integer intHere = Integer.valueOf(stringHere);
     
  3. Offline

    Drkmaster83

    A word of advice: There's an exception called NumberFormatException. If you surround your Integer.parseInt(string) with a try/catch, you can send the player messages for when they provide an actual String instead of numbers. Example:
    Code:
    int i = 0;
    try {
      i = Integer.parseInt(args[0]);
    }
    catch (NumberFormatException e) {
      player.sendMessage(ChatColor.RED + "You know darn well that \"" + args[0] + "\" is not a number!");
      return false;
    }
    
     
    hintss likes this.
Thread Status:
Not open for further replies.

Share This Page