3 Replies - 253 Views - Last Post: 11 September 2011 - 09:42 PM Rate Topic: -----

#1 velo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 26-June 11

Print everything in the array with string concatenation

Posted 11 September 2011 - 11:20 AM

The "theDeck" array is made of "Card" objects. I have a separate class called "Card" and I made a toString method for the "Card" class.

	/**
	 * Overriding the toString method
	 * 
	 * @return a string of deck
	 */
	public String toString() {
		String line = "";

		for (int i = 0; i <= theDeck.length ; i++) {
			String temp = temp + theDeck[i].toString() + " ";
		}
		return line;
	}




I know this is wrong, and I forgot how to solve it because I know that every time it goes through the "for loop" and makes a string object called temp and I lost information from the previous index position. Please help. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Print everything in the array with string concatenation

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Print everything in the array with string concatenation

Posted 11 September 2011 - 11:24 AM

I don't think you need the String temp, at least I haven't figured out why. You do want to collect all items in a String, and the line field will do fine. To do that, you'd use at line 10:

line += theDeck[i].toString() + " ";

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,105
  • Joined: 19-March 11

Re: Print everything in the array with string concatenation

Posted 11 September 2011 - 11:49 AM

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

	        String temp = temp + theDeck[i].toString() + " ";

This simply creates a new "temp" and assigns the current line to it, then throws it away.

To do it with string concatenation, you would either follow this with
"line+= temp +System.getProperties("line.separator");


(inserted a line separator assuming that you'd want cards on separate lines, delete if assumption is not valid)

or else, as Greg says, just assign
line += theDeck[i].toString() + " ";


However, you don't want to use String concatenation for this purpose. Get in the habit of using StringBuilder:

declare a new StringBuilder outside of the loop:

StringBuilder sb = new StringBuilder();


Then use

sb.append(theDeck[i].toString() + " ");


To avoid repeatedly creating and throwing away new String objects.

To get the contents, use the toString method, ie
System.out.println(sb.toString());

This post has been edited by jon.kiparsky: 11 September 2011 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 whocares?  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 08-September 11

Re: Print everything in the array with string concatenation

Posted 11 September 2011 - 09:42 PM

try to declare you "temp" variable outside the loop. thats it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1