简体   繁体   中英

Exception in thread “main” java.lang.NumberFormatException: For input string: “” in Java

I created a class in java to read a text file (.txt) and print on the screen the result on the screen. Script is reading the contents of the text file, but at the end it is displaying the message:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at com.desafioProgramacao.LerArquivo.main(LerArquivo.java:24)

I do not know why it displays the message. In the FINALLY class I tell it to close the FileReader and the BufferedReader if the contents of the file are null. Follow the Java code and screen prints.

public class LerArquivo {

private static final String NomeArquivo = "E:\\DesafioProgramacao\\matriculasSemDV.txt";

public static void main(String[] args) {

    FileReader fr = null;
    BufferedReader br = null;

    try {
        fr = new FileReader(NomeArquivo);

        br = new BufferedReader(fr);

        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            int num = Integer.parseInt(sCurrentLine);
            System.out.println(num);
        }
    } catch (IOException e) {
        e.printStackTrace();

    } finally {

        try {
            if (br != null) {

                br.close();

            }

            if (fr != null) {

                fr.close();
            }
        } catch (IOException ex) {

            ex.printStackTrace();
        }
    }
}}

例外

文件阅读器

The cause on the surface is that your read an empty string and want to parse it to int

For the code,you need check the sCurrentLine value

while ((sCurrentLine = br.readLine()) != null) {
    if(StringUtils.isNotBlank(sCurrentLine)){//StringUtils is from `commons-lang` 
     // or if(sCurrentLine.length()>0)
     int num = Integer.parseInt(sCurrentLine);
     System.out.println(num);
    }
}

For the txt file,you need to remove all the empty line at the end of the file

Your file contains an empty line (probably at the end).

Replace your while loop with:

while ((sCurrentLine = br.readLine()) != null && !sCurrentLine.isEmpty()) 

The problem is the last line, it is a blank space. You can do:

while ((sCurrentLine = br.readLine()) != null) {
    if (!sCurrentLine.isEmpty()) {
        int num = Integer.parseInt(sCurrentLine);
        System.out.println(num);
    }
}

The correct way to fix it is to catch that NumberFormatException and handle it properly, like that:

         try {
            int num = Integer.parseInt(sCurrentLine);
            System.out.println(num);
        } catch (NumberFormatException ex) {
            System.out.println("Error reading line: " + sCurrentLine);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM