📜  Java中的 ParsePosition setErrorIndex() 方法与示例

📅  最后修改于: 2022-05-13 01:55:22.341000             🧑  作者: Mango

Java中的 ParsePosition setErrorIndex() 方法与示例

Java.text.ParsePosition类的setErrorIndex()方法用于设置可能出现解析错误的索引。
句法:

public void setErrorIndex(int ei)

参数:此方法将整数 ei作为可能发生解析错误的参数。
返回值:此方法没有任何返回值。
以下是说明setErrorIndex()方法的示例:
示例 1:

Java
// Java program to demonstrate
// setErrorIndex() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing
            // new ParsePosition Object
            ParsePosition pos
                = new ParsePosition(1);
 
            // set index for the parsing error
            // using setErrorIndex() method
            pos.setErrorIndex(3);
 
            // getting the current
            // ParsePosition of
            int i = pos.getErrorIndex();
 
            // display result
            System.out.println(
                "index at which error occurred: "
                + Integer.toString(i));
        }
 
        catch (ClassCastException e) {
 
            System.out.println(e);
        }
    }
}


Java
// Java program to demonstrate
// setErrorIndex() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing
            // new ParsePosition Object
            ParsePosition pos
                = new ParsePosition(500);
 
            // set index for the parsing error
            // using setErrorIndex() method
            pos.setErrorIndex(3000);
 
            // getting the current
            // ParsePosition of
            int i = pos.getErrorIndex();
 
            // display result
            System.out.println(
                "index at which error occurred: "
                + Integer.toString(i));
        }
 
        catch (ClassCastException e) {
 
            System.out.println(e);
        }
    }
}


输出:
index at which error occurred: 3

示例 2:

Java

// Java program to demonstrate
// setErrorIndex() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing
            // new ParsePosition Object
            ParsePosition pos
                = new ParsePosition(500);
 
            // set index for the parsing error
            // using setErrorIndex() method
            pos.setErrorIndex(3000);
 
            // getting the current
            // ParsePosition of
            int i = pos.getErrorIndex();
 
            // display result
            System.out.println(
                "index at which error occurred: "
                + Integer.toString(i));
        }
 
        catch (ClassCastException e) {
 
            System.out.println(e);
        }
    }
}
输出:
index at which error occurred: 3000

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/ParsePosition.html#setErrorIndex-int-