# i need help with a lil bit of Java

This topic is 4645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i need help with this little bit of code. For some reason, the text area txt wont show the string after i set the text. it just keeps showing the old string. public void levToOut()//change matrix to one string, update txt { for(int i = 0; i < 35; i++) { for(int j = 0; j < 75; j++) { output += level[j]; } output += "\n"; } txt.setText(output); } output is the string that i send to the text area. level is a matrix of strings

##### Share on other sites
crap....the indents didnt work sorry

##### Share on other sites
Hi,

The setText call seems to be ok. I would suggest using System.out.println(output) so it also gets printed to the console, just so you can see what is contained in output.

I am assuming that you have output declared as a string as a class variable. This is generally a bad idea for using to output things. Generally a better idea is to have varialbes where you are just building up output as local to the method.

##### Share on other sites
to format your code you can use [code] and [/code] for small amounts of code, and [source] and [/source] to put large peices of code in a nice little formmatted scrollable window.

##### Share on other sites
The problem most likely lies somewhere else. Are you sure this bit of code is getting called? Adding a System.out.println statement in there somewhere to verify that it is getting called (or stepping through it in a debugger) is a good place to start.

A little tip for you: as a general rule, don't build up strings like that in loops. In Java, each string object is immutable, so everytime you append something to a string with the '+' operator, a new string might be created. Some VMs may avoid this behind the scenes by using a StringBuffer, but you can't depend upon it. Furthermore, you also create a new string for each member of the level array. So potentially, you are creating 150 new String objects in the inner loop each time it is run (one for each level[j] (75), and then a new one for output + the new level[j] string (another 75)), and in the outer loop you are creating 36 new strings (the "\n" is only created once since it's a literal, then you get 35 new ones for the output + "\n". So ultimately your two loops create 5286 new String objects (35 * 150 + 36).

To avoid that sort of thing, you can do this:

// I always like to initialize my string buffers with a multiple of 32,// and make it big enough so that it need not resize itself too often.// In this case, 1024 seems reasonableStringBuffer buf = new StringBuffer(1024);for(int i=0; i<35; ++i){   for(int j=0; j<7; ++j)   {      buf.append(level[j]);   }   buf.append("\n");}txt.setText(buf.toString());

This greatly reduces the number of String objects you create. If you are using Java 5, you can use the new StringBuilder class in place of StringBuffer (the StringBuffer methods are synchronized, StringBuilder methods are not).

##### Share on other sites
Quote:
 Original post by K4MEl30NHi, i need help with this little bit of code. For some reason, the text area txt wont show the string after i set the text. it just keeps showing the old string.

I don't see where the String "output" is being cleared. That method will just append data to the end of the String. If the text area is just big enough to fit "one matrix" so to speak then all the changes will be cut off.

And using StringBuffers would be more efficient like Aldacron said, but on PCs at least I'd wager the compiler optimizes that out.

shmoove

##### Share on other sites
Quote:
 Original post by shmooveAnd using StringBuffers would be more efficient like Aldacron said, but on PCs at least I'd wager the compiler optimizes that out.shmoove

Within loops, it can't - unless the compiler is surprisingly smart (and my tests show javac isn't smart enough - unless I completely misremember). Platform isn't an issue; if you write something for mobile, you're still compiling to the same VM spec using the same compiler.

##### Share on other sites
Quote:
 Original post by ZahlmanPlatform isn't an issue; if you write something for mobile, you're still compiling to the same VM spec using the same compiler.

Most phones don't have JIT so I figured it could make a difference. I haven't looked into it I could be wrong.

shmoove

##### Share on other sites
Oh, you're talking about optimization at *that* stage :) Never mind then, I couldn't even tell you how to test for that.