random.nextInt(args[0]); will not work?

Discussion in 'Plugin Development' started by WatermellonLOL, May 6, 2014.

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

    WatermellonLOL

    Hello there, I am trying to make a dice rolling command although it won't work if i type:
    Code:java
    1. if (commandLabel.equalsIgnoreCase("roll")) {
    2. if (args.length == 1) {
    3. Random random = new Random();
    4. int ranNum = 0;
    5.  
    6. for (int counter = 1; counter <= 1; counter++) {
    7. ranNum = 1 + random.nextInt(args[0]);
    8. }
    9. }else {
    10. sender.sendMessage(ChatColor.RED + "You typed the command in an invalid way.");
    11. }
    12. }


    .nextInt appears with a red line under it saying:

    [​IMG]

    I would like someone to explain this to me.
    - Thanks.
     
  2. Offline

    Gater12

    WatermellonLOL
    You want to parse the String to an Integer, using Integer.parseInt(String s)
    You can also put into a try catch statement and see if they are numerical (So they don't get errors if it isn't)
     
    WatermellonLOL likes this.
  3. Offline

    Gamecube762

    args is currently a list of strings, the Random.nextInt() wants a integer, not a string. You are going to have to change it to a integer. try something like this:
    int i = 0
    try -
    i = Integer.parseInt ~ args[0]
    - catch ( NumberFormatException ) - sendMessage~The argument needs to be a number!!!!! -

    This will try to change the string to a number, if it fails, it will catch the fail and send a message to the player.
     
  4. Offline

    RawCode

    for (int counter = 1; counter <= 1; counter++) {

    ehh?
     
  5. Offline

    Drkmaster83

    [​IMG]
     
    mazentheamazin and RawCode like this.
  6. Offline

    allestbs

    Code:java
    1. Integer.parseInt(args[0]);
     
Thread Status:
Not open for further replies.

Share This Page