Note that there are some explanatory texts on larger screens.

plurals
  1. POString concatenation in java is not working on one system
    text
    copied!<p>I believe that most of you would be thinking that this is the same question you have heard multiple times (and answered ) about string concatenation in Java. But trust me, it is different. In fact, so different that I am even hesitant in posting it here. But anyways, here it is. I have some piece of code which goes like:</p> <pre><code>public void handleSuccess(String result) { result = result.trim(); MessageBox.alert("Information","Result after trimming: '" + result + "'"); result = result.substring(result.indexOf('\n') + 1); MessageBox.alert("Information","Result after substring: '" + result + "'"); String returns = getReturns(); MessageBox.alert("Information","Returns: '" + returns + "'"); String action = getAction(); MessageBox.alert("Information","Action: '" + action + "'"); String finalResult = result + returns + action; MessageBox.alert("Information","Final result: '" + finalResult + "'"); } </code></pre> <p>Now the situation here is that, all of these : <strong>getReturns()</strong>, result and <strong>getAction()</strong> return non blank values, and in fact the string <strong>finalResult</strong> contains the concatenated value after the last line is executed. So, at Line 1, <strong>"result"</strong> contains <em>"12/03/2013|04-AERTY|"</em>. The value of result remains same at end of line 1,2. <strong>getReturns()</strong> returns value <em>12.4724</em>. So at end of line 3, <strong>finalResult</strong> contains <em>"12/03/2013|04-AERTY|12.4724"</em>. <strong>getAction()</strong> returns <em>"expt"</em>. So, at end of line 5, <strong>finalResult</strong> contains <em>"12/03/2013|04-AERTY|12.4724|expt"</em></p> <p>This is , when I debug or run the application in eclipse. As soon as build the same application on a UNIX system to generate a "war" file, and deploy the war on a tomcat server, the problem rears it's ugly head. When I run the application on the deployed war, the last line does not contain the concatenated value. So at the end of line 5, <strong>finalResult</strong> contains just <em>"12/03/2013|04-AERTY|12.4724"</em>. I expected it to contain <em>"12/03/2013|04-AERTY|12.4724|expt"</em> as it does while running in eclipse.</p> <p>I have tried <strong>stringbuffer</strong>, <strong>stringbuilder</strong> and the <strong>"+"</strong> operator as well, but nothing seems to work. I am not even getting an exception. Can somebody help me in fixing this or at least enlightening me in what I might be doing wrong here?</p> <p>Just to stress again, the code on eclipse(which is on a windows machine) and UNIX machine are exactly same. I have done a diff on them.</p> <p>Here is what I get after putting the message-boxes:</p> <ul> <li>Message-box 1: "Result after trimming: '12/03/2013|04-AERTY|'"</li> <li>Message-box 2: "Result after substring: '12/03/2013|04-AERTY|'"</li> <li>Message-box 3:"Returns: '12.4724'"</li> <li>Message-box 4:"Action: '|expt'"</li> <li>Message-box 5:"Final result: '12/03/2013|04-AERTY|12.4724|expt'"</li> </ul> <p>Message-box 5 output is the one I receive when I execute code using eclipse When running on deployed war, Message-box 1-4 have the same output as above, but Message-box 5 says: "Final result: '12/03/2013|04-AERTY|12.4724"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload