MessageFormat parse Example
MessageFormat class parse example. This example shows you how to use parse method.
MessageFormat class parse example. public Object[] parse(String source) throws ParseException Parses text from the beginning of the given string to produce an object array. The method may not use the entire text of the given string.
Here is the code
/*
* @ # Parse.java
* A class repersenting use to parse method
* of MessageFormat class in java.text package
* version 19 June 2008
* author Rose India
*/
import java.text.*;
public class Parse {
public static void main(String[] args) {
// Create a pattern for our MessageFormat object to use.
String pattern = "I bought {0,number,#} " +
"apples for {1,number,currency} " +
"on {2,date,dd-MMM-yyyy}.";
// Create a MessageFormat object and apply the pattern
// to it.
MessageFormat mFmt = new MessageFormat(pattern);
Object[] values = null;
try {
values = mFmt.parse(
"I bought 20 apples for $4.18 on 11-Nov-2004.");
} catch (ParseException ex) {
System.out.println(ex.toString());
}
// Print out the pattern being used for formatting
// and the formatted output.
System.out.println(mFmt.format(values));
}
} |
Output
java.text.ParseException: MessageFormat parse error!
I bought {0} apples for {1} on {2}. |
|