简体   繁体   English

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

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

I am currently working on a personal project outside of class and am running into some issues while reading in a text file into a linked list. 我目前正在课外开展个人项目,在阅读文本文件到链表时遇到了一些问题。 When reading in the first double I get a 当读到第一双时,我得到一个

java.lang.NumberFormatException: empty String

error. 错误。 I added a print line into the program to print out what I am trying to parse into a double and the variable is in fact, not empty, and is in fact a double. 我在程序中添加了一条打印行,打印出我想要解析为double的内容,而变量实际上并非空,实际上是一个双精度数。

Like I said above, I added a print line to print out the string I am trying to parse into a double and it seems to be okay. 就像我上面说的那样,我添加了一个打印行来打印出我想要解析成双字符串的字符串,它似乎没问题。 Here is the String that is read in and split into the array I am printing from: 这是读入并分割成我正在打印的数组的String:

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

I have to parse two strings into doubles and I only run into problems with the first one. 我必须将两个字符串解析为双精度数,而我只遇到第一个问题。 When I comment out the first double being parsed, the program runs with no problems. 当我注释掉第一个被解析的双子程序时,程序运行没有问题。 Here is the full output from running to program 这是从运行到程序的完整输出

 *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)*

The problems happens in the "Splitting the array into its appropriate temp variables" section in this line of code: 问题发生在这行代码中的“将数组拆分为适当的临时变量”部分:

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

Here is the code for the method this is in 以下是此方法的代码

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);

        }

    }

Edit: 编辑:

Here is a Minimal, Complete, and Verifiable example of the problem I am running into: 这是我遇到的问题的最小,完整和可验证的示例:

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]);

        }
    }
}

Here is the content of the text file mentioned in the code above: 以下是上述代码中提到的文本文件的内容:

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

Your text file likely contains empty lines. 您的文本文件可能包含空行。 You can either remove the new lines in the text file, change how the text file is created, or just skip the empty lines when you read it. 您可以删除文本文件中的新行,更改文本文件的创建方式,也可以在阅读时跳过空行。

This is how you skip the empty lines: 这是你跳过空行的方法:

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