猿问

Java BufferedReader 总是在第一次迭代时抛出

为什么我先得到 Not a number 异常,然后得到正确的输出?


import java.io.*;

import java.util.ArrayList;


public class readfile {

    public static void main(String args[]) {

        ArrayList<Integer> arr =new ArrayList<>();

        BufferedReader buff = null;

        FileInputStream fs = null;

        try {

            fs = new FileInputStream("/home/krishna/Documents/file/file");

            buff = new BufferedReader(new InputStreamReader(fs));


        String line = buff.readLine();

            while(line != null) {

                try {

                    arr.add(Integer.parseInt(line.trim()));

                }

                catch(NumberFormatException e) {

                    //System.out.println("Not a number");

                 e.printStackTrace();

                }

                line = buff.readLine();

            }

        }


        catch(FileNotFoundException e) {

            System.out.print(e);

        }

        catch(IOException e) {

            System.out.print(e);

         }

         sumOfArray(arr);

       }

     static void sumOfArray(ArrayList<Integer> arr) {

        int sum=0;

        for(Integer a:arr) {

            System.out.print(a+"\t");

            sum = sum+a;

        }

        System.out.println("Sum is : "+" "+sum);

        }

   }

该文件包含从 1 到 9 的数字,每个数字在新行中,开头没有空格或空行。


Stacktrace 打印以下异常


output:

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.mojang.readfile.main(readfile.java:18)

1   2   3   4   5   6   7   8   9   Sum is :  45


慕盖茨4494581
浏览 145回答 2
2回答

手掌心

在您的文件中,\n我认为最后一行有一个新行。请注意文件末尾没有新行。检查计数器或打开文件并删除最后一行。这意味着文件必须是这样的;// -->remove all if some char is here!!1\n2\n3\n4\n...9&nbsp; &nbsp;//--> there is no new line !!!!!或更改您的代码;if(line != null && !line.isEmpty()){&nbsp; &nbsp; &nbsp;arr.add(Integer.parseInt(line.trim()));}

慕工程0101907

您的输入中似乎有一个空行。我建议Scanner改用它,因为它会为您跳过空格。public class ReadFile {&nbsp; &nbsp; public static void main(String[] args) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; String file = "/home/krishna/Documents/file/file";&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> ints = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; try (Scanner in = new Scanner(new File(file))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (in.hasNextInt())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ints.add(in.nextInt());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sumOfArray(ints);&nbsp; &nbsp; }&nbsp; &nbsp; static void sumOfArray(List<Integer> ints) {&nbsp; &nbsp; &nbsp; &nbsp; long sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int a : ints) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a + "\t");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += a;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nSum is: " + sum);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答