Jtest logo




Contents  Previous  Next  Index

INTER.COS


String concatenation will not work in an internationalized environment

Description

This rule flags code that contains concatenated strings.

For code to be able to run in an internationalized environment, concatenated strings must not be used. This includes using the '.concat()' method with strings or using '+' or '+=' operators with strings. The reason for this is compound messages contain variable data and are difficult to translate. For example the message : "At 12:30 P.M on Jul 3, 2053, there was a disturbance in the Force."

Example

 import java.util.*;
 package INTER;
 public class COS{
   public void addstrings(){
       Date current_date = new Date(System.currentTimeMillis());
       String message = "At " + current_date + " there was a 
disturbance in the Force ";    // violation
       String message2 = "on planet 7";
       message = message.concat(message2);   // violation
       System.out.println(message);
   }
 }

Repair

Do not use '.concat()' or "+", '+=" operations with Strings. Instead, use the "MessageFormat" class to deal with Compound Messages as follows:

 Object [ ] arguments = { 
     new Integer(7),
     new Date(System.currentTimeMillis()),
      "a disturbance in the Force"
 };
 String message = MessageFormat.format( "At  (1,time) on ( 1, date) , 
there was 
 {2} on planet { 0,number,integer).", arguments);

Reference

http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft