I am trying to find a string from messages.properties
in an errorMessage
and if the errorMessage
has the string I need to replace it with corresponding value
I have messages.properties
as below
inv_date=INVOICE DATE
inv_id=INVOICE NUMBER
cl_id=CLIENT ID
lf_matter_id=LAW FIRM MATTER ID
inv_total_net_due=INVOICE TOTAL
(inv_start_date|invoice start date)=BILLING START DATE
(inv_end_date|invoice end date)=BILLING END DATE
inv_desc=INVOICE DESCRIPTION
units=LINE ITEM NUMBER OF UNITS
discount_amount=LINE ITEM ADJUSTMENT AMOUNT
total_amount=LINE ITEM TOTAL
(charge_date|charge date)= LINE ITEM DATE
acca_task=LINE ITEM TASK CODE
acca_expense=LINE ITEM EXPENSE CODE
acca_activity= LINE ITEM ACTIVITY CODE
(tk_id|time keeper id)=TIMEKEEPER ID
charge_desc=LINE ITEM DESCRIPTION
lf_id=LAW FIRM ID
(BaseRate|Rate|tk_rate)=LINE ITEM UNIT COST
tk_level=TIMEKEEPER CLASSIFICATION
cl_matter_id=CLIENT MATTER ID
My errorMesage
can have any of the (left side)string and I need to replace it with right side string value
Below are couple of sample error messages
String errorMesage1 = "Line 3 : Could not parse inv_date value"
String errorMesage2 = "Line : 1 BaseRate is a required field"
below is my method which conversts the error message
public static String toUserFriendlyErrorMessage(String message) {
ResourceBundle rb = ResourceBundle.getBundle("messages");
for(String key : rb.keySet()){
String header = rb.getString(key);
if(message.contains(key)) {
return message.replaceAll(key, rb.getString(key));
}
}
return message;
}
Below is the expected output: for errorMessage1 it works fine
System.out.println(toUserFriendlyErrorMessage(errorMessage1)); ==> Line 3 : Could not parse INVOICE DATE value
But for errorMessage2 its not working. It doesnt replace BaseRate
with LINE ITEM UNIT COST
System.out.println(toUserFriendlyErrorMessage(errorMessage2)); ==> Line : 1 BaseRate is a required field
Is there a way to find the occurance of multiple strings and replace it with its corresponding value?
For example: Find (BaseRate|Rate|tk_rate)
and replace the string with LINE ITEM UNIT COST
Also am wondering can this method simplified further in java 8?
See Question&Answers more detail:os