0

I am getting jumbled up with this and I might know the answer. How do I make my String[] array increment every time this function is called? Here is the function code:

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){

        //The /ticket command
        if(cmd.getName().equalsIgnoreCase("ticket")){
            //Gets amount of arguments
            int size_of_args = args.length;

            String ticket = null;
            String[] ticket_array;

            //Puts ticket into one string to be stored.
            for (int i=0; i<=size_of_args; i++){
                ticket = ticket + args[i];
            }


            return true;

            } else {return false;}
    }

I think I have to do a for loop, but I am very tired and this has been stumping me for quite a while. The function is not complete, so do not mention that I haven't used String commandLabel or CommandSender sender (sorry if that sounds rude). Thanks in advance!

P.S. I do not want to set a value for String[] ticket_array because the amount of tickets made should be as many as there are sent in.

2
  • What do you mean by "incrementing" the array? Commented Mar 4, 2012 at 4:38
  • @jacobm Sorry about my clarity. I mean increment the value at which ticket is stored. So the first time the function/method is called ticket_array[0] = ticket; and second time ticket_array[1] = ticket;and etc. Commented Mar 4, 2012 at 4:40

2 Answers 2

3

Consider using a List instance as class member, this way, you don't need to worry regarding the size, for example:

List<String> commandList = new ArrayList<String>(50);

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    //The ticket command
    if(cmd.getName().equalsIgnoreCase("ticket")){
        String ticket = "";
        // ommiting
        commandList.add(ticket);
        return true;
    } 
    else {
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I am new to this concept, what does this do and how do I access it in the future(like you access a string with ticket_array[10];)?
I cannot read that very well. I am rather new to Java. I am illiterate :D
Then that's your chance to learn :-P. seriously - how can I help with that?
@VinylScratch if you can't walk the walk - don't talk the talk. +1 to Binyamin.
|
0

I'm not sure if I'm understanding your question correctly but it sounds to me like you want to access a higher index of the array every time the function is called. If that's the case then you may want to consider using a static counter variable. this would have to be defined outside the scope of the function but it could be incremented during the function call

static int arrayCounter = 0;

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,            String[] args){
    //The ticket command     
    arrayCounter++;
    .
    .
    .
}

then use

ticket_array[arrayCounter];

to access the desired index

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.