ParsePosition getIndex() method in Java with Example
Last Updated :
22 Jan, 2020
The getIndex() method of java.text.ParsePosition class is used to retrieve the current parse position of this ParsePositon object .
Syntax:
public int getIndex()
Parameter: This method does not accepts any argument as parameter.
Return Value: This method returns the current parse position of this parse position object.
Below are the examples to illustrate the getIndex() method:
Example 1:
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
ParsePosition pos
= new ParsePosition( 1 );
pos.setErrorIndex( 3 );
int i = pos.getIndex();
System.out.println(
"current parse position: "
+ Integer.toString(i));
}
catch (ClassCastException e) {
System.out.println(e);
}
}
}
|
Output:
current parse position: 1
Example 2:
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
ParsePosition pos
= new ParsePosition( 1 );
pos.setIndex( 3 );
int i = pos.getIndex();
System.out.println(
"current parse position: "
+ Integer.toString(i));
}
catch (ClassCastException e) {
System.out.println(e);
}
}
}
|
Output:
current parse position: 3
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ParsePosition.html#getIndex–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...