Solved Help with string separation

Discussion in 'Plugin Development' started by SrNicks_, Jun 5, 2015.

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

    SrNicks_

    How can I do to separate the part of the message, and the numeric part in the same message?

    Example:
    /command SrNick banned for offence 20

    Code:
        String msg = "";
                for (int i = 1; i < args.length; i++) {
                    msg = msg + args[i] + " ";
                }
     
  2. Offline

    caderape

    @SrNicks_
    try Interger.parseint(args[i);
     
  3. Offline

    SrNicks_

    @caderape
    How do I get only this number, and soon after it removes the message?
     
  4. parse the int of the arg index.
     
  5. Offline

    Gamecube762

    @SrNicks_
    Either change how its formatted: /ban Gamecube762 20 Running round in circles while playing the banjo!

    or loop through all the args while trying to parse each one as an Integer and catching the NumberFormatException:
    Code:
    for (String : args) do
    try{i = Integer.parseInt(string)}
    Catch(NumberFormatException){//Oh no! this arg wasn't a number! Lets try the next one!}
     
  6. Offline

    ForsakenRealmz

    @SrNicks_ I'd agree with @Gamecube762 on this one. I would change the format of the command so the integer is in the second position, and the message is on the end. It will make it easier. You would then use
    Code:
    int time = Integer.parseInt( args[1] );
    But then again I would do a Try/Catch like above in-case the user doesn't enter a number, but instead a String.
     
  7. Offline

    teej107

    Just get the arguments 1 through the array length - 2 for the message. Then the last value of the array for the number string for you to parse.
     
  8. Offline

    mine-care

    You can always find a edged to remove all non numeric values but that will only work if there is just one number in the string else it will merge them.
     
  9. Offline

    SrNicks_

  10. Offline

    Gamecube762

    @SrNicks_ "it", there are two "it"s.

    For reformatting, all you have to do is tell everyone to type it as /ban <name> <time> <reason>. In your code, you would use args[0] for the name, arg[1] as the time, and arg[2] for the reason.

    For grabbing the integer:
    Code:
    String = "20"
    int = 0
    try {i = Integer.parseInt(string) }
    catch (NumberFormatException) { sender.sendMessage(int Is not a number!!)}
     
  11. Offline

    SrNicks_

Thread Status:
Not open for further replies.

Share This Page