Name: gm110360 Date: 10/31/2003 FULL PRODUCT VERSION : java version "1.4.1" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21) Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode) FULL OS VERSION : Microsoft Windows 2000 [Version 5.00.2195] A DESCRIPTION OF THE PROBLEM : If the command line argument (i.e, string value) includes new-line character('\n') then it is not being recognised as a single new-line ascii character. Rather, it is treated as two distinct ascii characters ('\' and 'n'). But if the same string is used in the java source code, instead of passing as command line argument, the new-line character is recognised as a single new-line ascii character. STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : Execute the sample source code... C:\> java Test "This\ncontains\nline" EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - Expected the new-line character to be recognised in both command-line argument and java source hard-code string. This is much like C/C++!!!!! This is much like C/C++!!!!! ACTUAL - First time, the command line argument is not recognised. But the second if condition succeeds. This ain't like C/C++!!!!! This is much like C/C++!!!!! ERROR MESSAGES/STACK TRACES THAT OCCUR : No errors or exceptions occurred. REPRODUCIBILITY : This bug can be reproduced always. ---------- BEGIN SOURCE ---------- /* Source code STARTS */ class Test { public static void main(String [] arrStrArgs) { String strTemp = arrStrArgs[0]; if( "This\ncontains\nline".equalsIgnoreCase( strTemp ) ) { System.out.println( "This is much like C/C++!!!!!" ); } else { System.out.println( "This ain't like C/C++!!!!!" ); } strTemp = "This\ncontains\nline"; if( "This\ncontains\nline".equalsIgnoreCase( strTemp ) ) { System.out.println( "This is much like C/C++!!!!!" ); } else { System.out.println( "This ain't like C/C++!!!!!" ); } System.exit( 0 ); } } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : I had to write a method to parse all the command-line arguments and replace the string "\\" + "n" with ascii character '\n'. Following is the method: /* Call this method like this: strText = replace( strText, "\\" + "n", '\n' ); strText = replace( strText, "\\" + "t", '\t' ); */ public static String replace( String strSource, String strFind, char chrReplace ) { // buffer to hold the target string after replacement is done. StringBuffer sbfTemp = new StringBuffer(); try { // for each occurrence of strFind in strSource, replace it with chrReplace. int intIndex = strSource.indexOf( strFind, 0 ); // check if there is any instace of strFind in strSource if( intIndex >= 0 ) { // holds the index from where the search is supposed to happen. int intStart = 0; // size of the source string int intTotalSize = strSource.length(); while( intStart < intTotalSize && ( ( intIndex = strSource.indexOf( strFind, intStart ) ) >= 0 ) ) { // check if strFind is at the beginning... i.e., at index intStart if( intIndex == intStart ) { /* * starts with strFind...just append chrReplace * to the target */ sbfTemp.append( chrReplace ); } else { // append the sub-string...plus chrReplace sbfTemp.append( strSource.substring( intStart, intIndex ) ); sbfTemp.append( chrReplace ); } // advance string index intStart = intIndex + strFind.length(); } // append the last portion of the source string. sbfTemp.append( strSource.substring( intStart ) ); } else { // strFind not found... just copy the text as it is. sbfTemp.append( strSource ); } } catch( Exception expGeneral ) { // in case of any exception, return the source string as it is. sbfTemp = new StringBuffer( strSource ); } return sbfTemp.toString(); } (Incident Review ID: 208529) ======================================================================
|