Solved Multi arguments

Discussion in 'Plugin Development' started by Urag, Aug 6, 2015.

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

    Urag

    I want to get arguments from second to end
    Something like
    Code:
    String s = from second argument to the end
     
    Last edited: Aug 6, 2015
  2. Offline

    Timbals

    Code:
    String s = ""; // first create an empty string
    for (int i = 0; i < args.length; i++) { // loop through all arguments
        if (i > 0) { // is true if it is not the first argument = everything after the the first argument
                s = s + args[i]; // add everything together
        }
    }
    
     
  3. Offline

    Urag

    @Timbals
    When i do "/ban urag first second third" I see "firstsecondsthird", how to change your code to get spaces between args?
     
  4. Offline

    MrBlackIsBack

    @Urag There are many ways to achieving this, however the one I always use, and recommend, is looping through all, and appending it.

    For example:
    Code:
    StringBuilder = new StringBui..
    
    for i = starting argument; i > length? i++
        s.append( args[i] ).append(a space);
    
    finalString = s.toString();
    Output:
    - If the arguments are: "Mr Black is actually back."
    - finalString would equal: "Mr Black is actually back."

    Hope this helps, more information on stringbuilders can be viewed here: https://docs.oracle.com/javase/tutorial/java/data/buffers.html
     
    Urag likes this.
  5. Offline

    Urag

    @MrBlackIsBack
    ========================================================================================
    I don't know how to fill it :/
    I have made this, but...
    [​IMG]

    Can you help me more?
     
    Last edited: Aug 6, 2015
  6. Offline

    MrBlackIsBack

    Okay, here is a run through:
    - 'starting argument' : args[0] is equal to your first argument, right? For example: "/Mr Black" Mr is your command, black is your first argument. We want to start at our first argument, right?
    - 'length' : The amount of arguments we have. args#length returns the amount of arguments we have used in our command.
    - 's.append(args).append(a space) ' : " " <-- Is a space, right?
    - finalString = s.toString(); : Well, we know #toString() returns a string, so it's obvious that finalString would have to be a string.

    Also, you might want to rename your already made stringbuilder (StringBuilder sb = new StringBuilder()) too 's' so you don't confuse yourself.

    Head on over the the official Java tutorials for further reading (https://docs.oracle.com/javase/tutorial/)
     
    Urag likes this.
  7. Offline

    Urag

    @MrBlackIsBack
    What made i wrong?

    Code:
                StringBuilder s = new StringBuilder();
                       
                for(i = args[0]; i > length? i++){
                    s.append( args[i] ).append(a space);
                }
                finalString = s.toString();
    Eclipse underlines line 3. and 4.
     
  8. Offline

    MrBlackIsBack

    @Urag Read my above post. I explained everything.
     
  9. Offline

    Urag

    @MrBlackIsBack
    Oh i have just looked at @Timbals' code.
    Is it all correct now?
    Code:
                StringBuilder s = new StringBuilder();
                     
                for (int i = 1; i < args.length; i++){
                    s.append(args[i]).append(" ");
                }
                String finalowa = s.toString();
    ========================================================================================
    This code works, but It's better to ask.
     
    Last edited: Aug 6, 2015
Thread Status:
Not open for further replies.

Share This Page