Hi,

I have a quick question. Why would I be getting this: Ljava.lang.String;@24c21495 instead of the information at that address? I have println as my system.out. I am not sure how to format a String[] to make sure I get the information rather than the address.

Thank you,

lynnajoe

When you pass an object o to println() as its parameter, it typically tries to print it by calling String.valueOf(o) to get a String value. That value is going to be the value returned from calling o.toString(). If that object overrides Object.toString(), then that return value will be whatever the creator of the class specified as the representative String. If that object does not override toString(), then it'll call all the way up to Object - Object.toString() returns the memory address of the object in question.
Array, you'll not be surprised to learn, does not override toString(), so you're looking at the memory address of that array. If you want the contents of the array, you have to get them by specific reference to some element, or looping over the whole array.

(but see this discussion for an exception)

Hi,

I saw your see this discussion notation but I am having a hard time formatting these arrays. I want it to show the product and then compute the total. So far I have tried printf,print,println and nothing seems to work. I was hoping for a tutorial somewhere which shows when to use this format and when to use another format. I can not find one. Oh and thank you.

lynnajoe

Okay, let's say you have an array of String, called array, and you want to print the contents. You can't print it directly, like println(array), you have to loop over the array either in a standard for loop:

for (int i=0; i<array.length; i++) {}

or an enhanced for loop:

for (String s:array){}

In the body of the loop, you're going to put a print statement that prints each String as you go through the loop. You can format that String any way you like.

Thank you once again!

lynnajoe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.