ParsePosition getErrorIndex() method in Java with Example
Last Updated :
22 Jan, 2020
The getErrorIndex() method of java.text.ParsePosition class is used to retrieve the index at which parse error may occur.
Syntax:
public int getErrorIndex()
Parameter: This method does not accepts any argument as a parameter.
Return Value: This method returns the index at which parse error may occur.
Below are the examples to illustrate the getErrorIndex() 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( 0 );
pos.setErrorIndex( 3 );
int i = pos.getErrorIndex();
System.out.println(
"index at which parse error may occur: "
+ Integer.toString(i));
}
catch (ClassCastException e) {
System.out.println(e);
}
}
}
|
Output:
index at which parse error may occur: 3
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( 0 );
int i = pos.getErrorIndex();
if (i != - 1 )
System.out.println(
"index at which parse error may occur: "
+ Integer.toString(i));
else
System.out.println( "error index is not set" );
}
catch (ClassCastException e) {
System.out.println(e);
}
}
}
|
Output:
error index is not set
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ParsePosition.html#getErrorIndex–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...