JavaRush /Java Blog /Random EN /MessageFormat class
Виталий Рыжаков
Level 40
Москва

MessageFormat class

Published in the Random EN group
In this article I want to talk about the class MessageFormatthat is used in task 2412 (task2412). Class MessageFormat - 1The class MessageFormatis designed to create strings. This Java class takes a set of objects, formats them, and then inserts the formatted strings into the template at the appropriate locations. This is a kind of alternative (or even addition) to the static method String.format. First, a simple example of using this Java class without creating an object, but using a static method:
int planet = 7;
String event = "a disturbance in the Force";
String result = MessageFormat.format(
        "At {0, time, medium} on {0, date}, there was {1} on planet {2, number, integer}.",
        new Date(), event, planet);
System.out.println(result);
Here a static method is called MessageFormat.format, to which the string template and, in fact, objects that will be inserted into places delimited by parentheses are passed as arguments {}. The position of the object starting at is specified in parentheses 0, as well as the type of formatting, if any. The output will be like this:
At 21:25:54 on 28 апр. 2018 г., there was a disturbance in the Force on planet 7.
The following example already creates an object of the class MessageFormat:
int fileCount = 1273;
String diskName = "MyDisk";
Object[] testArgs = {fileCount, diskName};

MessageFormat form = new MessageFormat(
        "The disk \"{1}\" contains {0} file(s).");

System.out.println(form.format(testArgs));
When a class object is created, MessageFormata string template is passed to its constructor. Next, when calling a method formaton an object, an array of objects that will be inserted into the string template is passed there as an argument. The output will be like this:
The disk "MyDisk" contains 1 273 file(s).
It is also possible to make sure that the required text is selected depending on the value of the variable. A kind of implementation of the operator if...else, only using the class ChoiceFormat. Here's the code:
MessageFormat form = new MessageFormat("Я могу {1} {0}.");
int count = 2;
String exercise = "подтянуться";
Object[] testArgs = {count, exercise};

double[] filelimits = {0,2,5};
String[] filepart = {"{0} раз","{0} раза","{0} раз"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
form.setFormatByArgumentIndex(0, fileform);

System.out.println(form.format(testArgs));
This code, depending on the value of the variable, countchanges the output of the line as follows:
  • if count = 1the conclusion is like this

    Я могу подтянуться 1 раз.
  • if count = 2the conclusion is like this

    Я могу подтянуться 2 раза.
  • if count = 7the conclusion is like this

    Я могу подтянуться 7 раз.
This code creates an array double[] filelimitsthat specifies the limits of values ​​at which the output of strings will change. And the array String[] filepartindicates the very variants of strings that can be used. The conditions for selecting a line option from the variable value are determined as follows: the option will be selected filepart[j]if filelimits[j] =< count < filelimits[j+1]. Next, an object is created and arrays and ChoiceFormat fileformare passed to its constructor . With the method, we tell the object that when the method is called , then for index 0 in the row template, use the formatting that was specified in the object . Fine. In principle, you might already have the idea of ​​never using a Java class , but know that this is not all the bells and whistles. There is this code: double[] filelimitsString[] filepartform.setFormatByArgumentIndex(0, fileform)MessageFormat formformatChoiceFormat fileformMessageFormat
MessageFormat pattform = new MessageFormat("There {0} on {1}.\n{2} {2}");
int count = 0;
Date date = new Date();
Object[] testArgs = {count, "ADisk", date, date};

double[] filelimits = {0,1,2};
String[] filepart = {"are no files","is one file","are {0} files"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);

Format[] testFormats
        = {fileform, null, DateFormat.getDateInstance(DateFormat.SHORT), DateFormat.getTimeInstance(DateFormat.SHORT)};
pattform.setFormats(testFormats);

System.out.println(pattform.format(testArgs));
Here is the main feature in the array Format[] testFormats. In this array we add objects (implementing the abstract class Format) that are responsible for formatting the objects defined in the array Object[] testArgs. Format[] testFormatsIf the object does not need formatting, then null. pattform.setFormats(testFormats)Next, we use the method to tell the object MessageFormat pattformthat all indexes in the row template should use the formatting defined in the array Format[] testFormats. For this example, the output would be:
There are no files on ADisk.
28.04.18 22:10
That's all, and I hope that you will not have any difficulties when solving problem 2412.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION