0

I have a piece of code that has an array passed to it in the form of args[]. args will contain data such as ["Hello", "guys"].

I need to use the sendMessage method to display chat in-game and where I have attempted to create a regular string from my array, what is sent in chat is a '[Ljava.lang.String;@' message instead.

String Msg = new String (args.toString());

OnlinePlayers.sendMessage(ChatColor.GREEN + Player.getName() + ": " + Msg);

Do you know how I can change this code so that the text from the array is put into one string and printed as words and not a weird format that no one understands?

======

New Version

String NewMsg = null;

String Msg = java.util.Arrays.toString(args);

for( int i = 0; i <= Msg.length() - 1; i++) {

    NewMsg = (args[i] + " ");

}

OnlinePlayers.sendMessage(ChatColor.GREEN + Player.getName() + ": " + NewMsg);

I'm now just getting errors and it isn't working at all. I thought this would get the amount of separate words from 'Msg.length()' and then iterate through the for loop and add a word plus a space to the 'NewMsg' string each loop.

1
  • "...what I believe to be an arraylist passed to it in the form of args[]..." ArrayList != array. Commented Aug 26, 2013 at 22:50

1 Answer 1

5

I have a piece of code that has what I believe to be an arraylist passed to it in the form of args[].

Nope, you're not dealing with an ArrayList at all but an array. That's what String[] args is declared to be and what it in fact is.

I need to use the sendMessage method to display chat in-game and where I have attempted to create a regular string from my arraylist, what is sent in chat is a '[Ljava.lang.String;@' message instead.

and what you're seeing is the expected result when you call toString() on a String array. Don't do this. Either use java.util.Arrays.toString(myArray) or loop through the array extracting each String item you encounter from the array from inside of the loop.

e.g.,

String msg = java.util.Arrays.toString(args);
OnlinePlayers.sendMessage(ChatColor.GREEN + Player.getName() + ": " + msg);

As an aside, please learn and use Java naming conventions. Variable names should begin with a lower case letter and classes, interfaces, and enums with an upper case letter. This will help prevent confusion about your code.

String Msg = new String (args.toString());

Aside number 2, don't use new String(...) unless you have a strong reason to do so. This will prevent Java from using an already existing String from the String pool, cluttering up your heap with unnecessary objects. A simple foo.toString() is all that would be needed in place of new String(foo.toString()).


Edit regarding:

String NewMsg = null;

String Msg = java.util.Arrays.toString(args);

for( int i = 0; i <= Msg.length() - 1; i++) {

    NewMsg = (args[i] + " ");

}

No, you would want to use java.util.Arrays.toString(...) or a for loop, not both. If you use a for loop your array needs to stay an array. Also, what good is the length of the Msg String as the for loop's max index count?

Instead your logic should be (pseudocode)

Declare a String variable, msg. Assign "" to it.
For int variable i goes from 0 to less than the length of the args array
  concatenate the i'th args item to the msg String.
use your msg String as needed.
Sign up to request clarification or add additional context in comments.

2 Comments

Now in the chat the message appears as "Player: ['Hello', 'guys']", how could I adapt this code so that it appears "Player: Hello guys". I need it to work with any amount of words entered.
@user2719632: create your String with a for loop. I have faith that you will be able figure out the code for this. :) Why not give it a try, and if your code doesn't work, paste your attempt as an edit to your question above.

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.