1

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

Exception

File Reader

2
  • 1
    Looks like your text file has an extra line at the end. Commented Apr 19, 2018 at 15:38
  • Maybe you got an empty line at the end of your file Commented Apr 19, 2018 at 15:38

4 Answers 4

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

Why not !sCurrentLine.isEmpty()? No need to involve Apache Commons for such a simple problem.
Because need to check if it has more than one empty space and I am not sure if sCurrentLine.isEmpty() can check for it
Why not just call parseInt(..) catch that Exception instead of considering all possible cases in advance?
1

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

Replace your while loop with:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.