I want to get arguments from second to end Something like Code: String s = from second argument to the end
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 } }
@Timbals When i do "/ban urag first second third" I see "firstsecondsthird", how to change your code to get spaces between args?
@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
@MrBlackIsBack ======================================================================================== I don't know how to fill it :/ I have made this, but... Can you help me more?
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/)
@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.
@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.