异构文本文件中的异常

时间:2015-01-31 21:47:14

标签: java parsing exception inheritance text-files

public static void main(String[] args) throws FileNotFoundException
{
printHeading(); 

    Scanner file1 = new Scanner(new File("C:\\Users\\nupur\\workspace\\Project2\\src\\EmployeesIn.dat")) ;
    System.out.println("New Employee added: Pryce, Lane");
    System.out.println("New Wages: ");
    while(file1.hasNextLine())
    {
          double total =0;
          String line=file1.nextLine();
          Scanner lineData = new Scanner(line);     
          String name = lineData.next().trim();
          int comma = name.indexOf(',');
          String last = name.substring(0, comma);
          String first = lineData.next().trim();
          char status = (lineData.next().trim()).charAt(0);
          double wage = Double.parseDouble((lineData.nextLine().trim()));

它给了我这个错误:新工资:

Exception in thread "main" java.lang.NumberFormatException: For input string: "5.00Baird,   Joey h 7.50Kinsey,  Paul h 8.00Olson,   Margaret                        s 15000Campbell,    Peter           s 20000Draper,  Donald s 40000Sterling, Roger s 45000Cooper,    Bertram             s 50000"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at Project2.main(Project2.java:38)

3 个答案:

答案 0 :(得分:1)

您正在尝试解析最后一行中不包含double的String。打印您实际尝试解析的内容的输出以跟踪您的错误。

答案 1 :(得分:0)

你试图解析“5.00Baird,Joey h 7.50Kinsey,Paul h 8.00Olson,Margaret s 15000Campbell,Peter s 20000Draper,Donald s 40000Sterling,Roger s 45000Cooper,Bertram s 50000”作为双倍。

因为这不是一个数字,所以你会得到例外。

答案 2 :(得分:0)

这一切都非常混乱。

你做这样的事情:

public static void main(String[] args) throws FileNotFoundException {
    printHeading(); 

    Scanner file1 = new Scanner(new File("C:\\Users\\nupur\\workspace\\Project2\\src\\EmployeesIn.dat")) ;
    System.out.println("New Employee added: Pryce, Lane");
    System.out.println("New Wages: ");
    while(file1.hasNextLine()) {
        System.out.println("Wage = " + file1.nextLine().replaceAll("[\\D]", "")) //replace all non-digit characters with blank
    }
}