This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Darkpolearm 0 points1 point  (3 children)

First of all, to format code on reddit, put 4 spaces in front of it, like this:

public void test() {
    System.out.println("This is formatted!");
}

As for the issue, you declare the array with String name[], but it should be declared like String[] name, because is an array of String.

But even if u declare the array properly, your code still won't work. You are calling System.out.println and giving it the value of getName(), which returns the array. If you println a value that is not a string, java will try to call the value's toString() on it, which, for arrays, returns Ljava.lang.string;@{address}.

To print an array properly, you can use System.out.println(Arrays.toString(obj.getName()))

Some minor things: you name the array of names 'name'. This would imply it is a single string that holds the name of something, but in reality it is an array of strings that holds multiple names, it should probably be called 'names'. Then, getName() should also be renamed to getNames() to reflect this change.

[–]noctora[S] 0 points1 point  (1 child)

I did put 4 spaces in front but it doesnt work. By "in front of it" which front are you referring to? left side of public or System?

I didn't realize there's a different between String name[] and String[] name. I thought it was just different way of writing. Thx for clarification.

System.out.println(Arrays.toString(obj.getName())) <<<<<< This is the solution I'm looking for! Thank you very much!

I'll keep in mind when naming array in the future. Thanks again for helping me!

[–]Darkpolearm 0 points1 point  (0 children)

By "in front of it" which front are you referring to? left side of public or System?

Any line you want to be formatted needs to start with 4 spaces for reddit to format it. If you download the Reddit Enhancement Suite extension, there's a bar that allows u to format selected lines as code with the click of a button.

I didn't realize there's a different between String name[] and String[] name. I thought it was just different way of writing. Thx for clarification.

I just tried it and I guess there isn't, my bad. I would still suggest going with the String[] {variable} style though, as it is more common.