Domanda

I have looked at this link, and can't see what I am doing wrong. I am using MessageFormat to get a property from a file, and want to make the property dynamic depending on whether the integer passed in is 1 or higher. The property in the file looks like this:

Prop1=Invalid password entered. You have {0,number,integer} {0,choice,1#attempt|1<attempts} remaining.

In the code, I read this property in, and then pass an array of arguments to MessageFormat.format() (I pass an array for consistency across all properties. In this case, the only object present is the string representation of an integer value) Whilst debugging the code, I can see that this string gets read, but MessageFormat.format() doesn't seem to format the string as expected. The result looks like this:

"Invalid password entered. You have {0,number,integer} {0,choice, 1#attempt|1<attempts} remaining."

Can anybody tell me what I'm doing wrong? As I stated earlier, I am passing in the string representation of the integer value, but I assumed that the code above would be able to handle it. Am I mistaken in my understanding?

È stato utile?

Soluzione

I hope, the snippet below might resolve the issue.

String msg  = "Invalid password entered. You have {0,number,integer} {0,choice,   1#attempt|1<attempts} remaining.";
int attemptCount = 1; 
System.out.println(MessageFormat.format(msg, attemptCount, attemptCount));

the result is,

Invalid password entered. You have 1 attempt remaining.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top