Spread the love

Class FieldPosition in Java

Spread the love

Class FieldPosition in Java

The FieldPosition class encapsulates information about fields in formatted output. The fields in a particular type of formatted output are identified by constants. A FieldPosition object contains its field type and the field’s position within the formatted output. The field position is specified by the index of the first character in the field and the index of the last character in the field.

We can use a FieldPosition object to find the position of a particular field in a formatted string.
Consider the following code:

NumberFormat nf = NumberFormat.getInstance();
StringBuffer sb = new StringBuffer();
FieldPosition fp = new FieldPosition(NumberFormat.FRACTION_FIELD);
nf.format(-1234.56, sb, fp);
System.out.println(new String(sb));
System.out.println("FRACTION_FIELD goes from " + fp.getBeginIndex()+ " to " + fp.getEndIndex() + ".");

This code produces the following output:
-1,234.56
FRACTION_FIELD goes from 7 to 9.

The structure of the class FieldPosition is given by

public class java.text.FieldPosition extends java.lang.Object {
// Constructors
public FieldPosition(int field);
// Instance Methods
public int getBeginIndex();
public int getEndIndex();
public int getField();
}

The details of the class structure are given as follows:

public FieldPosition(int field);

public FieldPosition(int field) constructor creates a FieldPosition object that represents the given field.

Parameter
field – A field constant.

public int getBeginIndex();

public int getBeginIndex() method returns the beginning index of the field that is represented by this FieldPosition.

This method returns the beginning index.

public int getEndIndex();

public int getEndIndex() method returns the ending index of the field that is represented by this FieldPosition.

READ  Class ContentHandler in Java

This method returns the ending index of this FieldPosition.

public int getField();

public int getField() method returns the field constant of this FieldPosition.
This method returns the field constant of this FieldPosition.

READ  Dangling Else Problem in Java

Apart from these FieldPosition class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special keyword called...

Comments

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love