4 Replies - 21194 Views - Last Post: 29 November 2008 - 02:05 PM Rate Topic: -----

#1 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

String [] giving exception: [Ljava.lang.String;@1a758cb

Post icon  Posted 29 November 2008 - 11:32 AM

My method has to return a String[].

When I added my data into the array I used this:


public String[] weDontHave() 
{
//I have created a list of items with Status: "i" in-stock, "o" out of //stock		
//the number of array elements is dictated by using notOnHand()

				String[] out = new String[notOnHand()];
		//this counts the number of elements in the array
		int size = 0;
		
		for(Node current = head; current != null; current = current.link)
		{
			if(current.item.getStatus().equals("o) "))
			{
			//adds the item to the array
				 out[size] = current.item.getName().toString() + current.Item.getStatus().toString() + current.item.getPrice().toString();				
				size++;
			}	
		}
		
		return out;
	}




So, if I try to print:

System.out.println(out[0]);//I get the correct String printed
//if I use
System.out.println(out[size]); //I get null
//if I use
System.out.println(out);//I get the [Ljava.lang.String;@1a758cb repeated
//




The testing program that uses a Scanner and user input, I've been given calls (is that the right term?) this method directly from main...

else if (action == 6)
	System.out.println(inventory.weDontHave());
//when I enter 6, I get [Ljava.lang.String;@1a758cb not the printed array.



I'm sure it's something simple...do I need to write another method?

Thank you in advance for reading this!

Is This A Good Question/Topic? 0
  • +

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: String [] giving exception: [Ljava.lang.String;@1a758cb

Posted 29 November 2008 - 12:14 PM

Well what exactly are you saying when you say System.out.println(out)? Well you are not saying print each of the strings in the array, you are saying print this object which is an array. What the error is telling you is the equivalent of print an array itself, not what is in the array.

Think of it like this... if we were to setup an integer and assign it 5. You use it in a print and it would print 5. But what if we were to simply pass an integer object ITSELF to the print. It would give you something similar to what you are seeing.

So you don't want to print the array object, you want to print whats inside that array. So the only way to do this is to loop through the array and access each element individually like out[0] or out[1]. Arrays themselves are only pointers to their data in memory.

So your function is right in that it returns String[]. That is all good. You just need to loop through the result instead of printing the result directly...


String[] results = inventory.weDontHave();

// Loop through the array, not print the array object itself.
for (int i = 0; i < results.length; i++) {
     System.out.println(results[i]);
}



Hope this makes a bit of sense. The array is only the structure, you want the data in that structure.

Good luck.

"At DIC we be array printing code ninjas... we also print trash stories on Britney Spears." :snap:
Was This Post Helpful? 1
  • +
  • -

#4 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: String [] giving exception: [Ljava.lang.String;@1a758cb

Posted 29 November 2008 - 01:45 PM

Thank you Martyr2!

That makes total sense -I forget that the array is a structure - didn't know how to properly get the data to print.

What I will be doing eventually is having this print into a GUI in a scrollable list (is that the right way to say it?).

The example in my notes shows it being cast into a String like this:

private void model()
{
   while(!model.isEmpty()) //model is the DefaultListModel

		 model.remove(0); //remove is an action listener/button
	//backend is the class that makes the array	  
	for(int i = 0; i < backend.size(); i ++) 
	  model.addTo((String).backend.get(i));
		  


And THANK YOU again! I will try this right away.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: String [] giving exception: [Ljava.lang.String;@1a758cb

Posted 29 November 2008 - 01:52 PM

Well when you put them into the defaultlistmodel they will be converted to objects. So when you get them back out, they will be objects still. So yes, you will have to cast them back to string before you can print them out. You may also be able to simply call toString on each of the returned objects and it will return the string value.

Try the tostring method first, if that seems to be giving you trouble, just cast them to String and you will be golden.

One side note... you will want to delete the period between (String) and "backend". It should be (String) backend.get(i) and NOT (String).backend.get(i).

Hope things work out for ya. :)
Was This Post Helpful? 1
  • +
  • -

#6 kiboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: String [] giving exception: [Ljava.lang.String;@1a758cb

Posted 29 November 2008 - 02:05 PM

Whoops! Thanks for catching that! :blush:

And another chorus of "thank you"s for explaining how it will go into the GUI (which will be my first attempt at one from scratch!)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1