簡體   English   中英

“java.lang.NumberFormatException:empty String”,非空字符串

[英]“java.lang.NumberFormatException: empty String” with a not empty String

我目前正在課外開展個人項目,在閱讀文本文件到鏈表時遇到了一些問題。 當讀到第一雙時,我得到一個

java.lang.NumberFormatException: empty String

錯誤。 我在程序中添加了一條打印行,打印出我想要解析為double的內容,而變量實際上並非空,實際上是一個雙精度數。

就像我上面說的那樣,我添加了一個打印行來打印出我想要解析成雙字符串的字符串,它似乎沒問題。 這是讀入並分割成我正在打印的數組的String:

500.0 % 04/05/2019 % This is paycheck 1 % true % 49.5

我必須將兩個字符串解析為雙精度數,而我只遇到第一個問題。 當我注釋掉第一個被解析的雙子程序時,程序運行沒有問題。 這是從運行到程序的完整輸出

 *File loading*


    *500.0*

    *Exception in thread "main" java.lang.NumberFormatException: empty String*
        *at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)*
        *at sun.misc.FloatingDecimal.parseDouble(Unknown Source)*
        *at java.lang.Double.parseDouble(Unknown Source)*
        *at fileHandling.readPaycheck(fileHandling.java:194)*
        *at UserMenu.main(UserMenu.java:20)*

問題發生在這行代碼中的“將數組拆分為適當的臨時變量”部分:

payCheckAmount = Double.parseDouble(tempArray[0]);

以下是此方法的代碼

public void readPaycheck(LinkedList<PayCheck> paycheck) throws IOException {

        // Declare Variables
        Scanner sc = new Scanner(payChecks); // Scanner used to read in from the payChecks text file
        String temp; // A string used to hold the data read in from the file temporarily
        String[] tempArray; // A String array used to temporarily hold data from the text file
        double payCheckAmount; // A double holding the amount of the paycheck
        String paycheckDate; // A string holding the date of the paycheck
        String description; // A string holding a description of the paycheck
        boolean payCheckSplit; // A boolean stating if the paycheck has been split or not
        double amountUnSplit; // A double

        // A while loop that runs while the text file still has data in it
        while (sc.hasNextLine()) {

            // Reading in a new line from the paycheck file
            temp = sc.nextLine();
            // Splitting the line into an array
            tempArray = temp.split(" % ");

            // Temp output used for testing of the issue at hand
            System.out.println(tempArray[0]);

            // Splitting the array into its appropriate temp variables
            payCheckAmount = Double.parseDouble(tempArray[0]);
            paycheckDate = tempArray[1];
            description = tempArray[2];
            payCheckSplit = Boolean.parseBoolean(tempArray[3]);
            amountUnSplit = Double.parseDouble(tempArray[4]);

            // putting the temp variables into a temp paycheck object
            PayCheck tempCheck = new PayCheck(payCheckAmount, paycheckDate, description, payCheckSplit, amountUnSplit);
            paycheck.add(tempCheck);

        }

    }

編輯:

這是我遇到的問題的最小,完整和可驗證的示例:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class test {

    public static void main(String[] args) throws IOException {

        // Declare Variables
        File payChecks = new File("C:\\Users\\zwtw\\Documents\\paychecks.txt");
        Scanner sc = new Scanner(payChecks); 

    while (sc.hasNextLine()) {

            String temp = sc.nextLine();

            String[] tempArray = temp.split(" % ");

            System.out.println(tempArray[0]);

            // Splitting the array into its appropriate temp variables
            double payCheckAmount = Double.parseDouble(tempArray[0]);
            String paycheckDate = tempArray[1];
            String description = tempArray[2];
            boolean payCheckSplit = Boolean.parseBoolean(tempArray[3]);
            double amountUnSplit = Double.parseDouble(tempArray[4]);

        }
    }
}

以下是上述代碼中提到的文本文件的內容:

500.0 % 04/05/2019 % This is paycheck 1 % true % 49.5
450.0 % 04/09/2019 % This is paycheck 2 % true % 49.75

您的文本文件可能包含空行。 您可以刪除文本文件中的新行,更改文本文件的創建方式,也可以在閱讀時跳過空行。

這是你跳過空行的方法:

while (sc.hasNextLine()) {

        String temp = sc.nextLine();

        if (temp.equals("")) { continue; } // <--- notice this line
        String[] tempArray = temp.split(" % ");

        System.out.println(tempArray[0]);

        // Splitting the array into its appropriate temp variables
        double payCheckAmount = Double.parseDouble(tempArray[0]);
        String paycheckDate = tempArray[1];
        String description = tempArray[2];
        boolean payCheckSplit = Boolean.parseBoolean(tempArray[3]);
        double amountUnSplit = Double.parseDouble(tempArray[4]);

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM