列表读取应用程序中的Java NumberFormatException

时间:2017-11-06 04:27:55

标签: java

我为一个程序编写代码,该程序读取包含list numbers的文件,并为列表中的每个数字输出下一个更大的数字。我正在使用eclipse进行这个项目,当我运行程序时,我遇到了一个错误,我似乎无法解决它。

我得到的错误是:

Exception in thread "main" java.lang.NumberFormatException: For input string: 
"78,22,56,99,12,14,17,15,1,144,37,23,47,88,3,19"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at numbers.main(numbers.java:25)

这是我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class numbers {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> nextList = new ArrayList<Integer>();
        File file = new File("list.txt");

        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;
// read the list of number from file

            while ((text = reader.readLine()) != null) {
                list.add(Integer.parseInt(text));
            }
// loop through each number
            for (int i = 0; i < list.size(); i++) {
                int num = list.get(i);
                int max = Integer.MAX_VALUE;
// get the next max value of each number
                for (int j = 0; j < list.size(); j++) {
                    if (num < list.get(j) && list.get(j) < max) {
                        max = list.get(j);
                    }
                }
                nextList.add(max);
            }
// print the numbers
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i) + " : " + nextList.get(i));

            }
        } catch (FileNotFoundException e) {
// if file not found
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
// close the file at the end
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

您需要在逗号(,)上拆分(并修剪())您的行,然后解析您获得的所有元素。 希望这能解决您的问题。

答案 1 :(得分:1)

问题是您正在尝试将具有多个数字的字符串转换为单个整数,这会导致您的异常。试试这个:

while ((text = reader.readLine()) != null) {
                String [] textArray = text.split(",");
                for (int i = 0; i < textArray.length; i++) {
                    list.add(Integer.parseInt(textArray[i]));
                }
            }

答案 2 :(得分:1)

String line="";
while ((text = reader.readLine()) != null) {
line=text; //read the text
}
line.trim();
// loop through each number
for(String num:line.split(",")){
list.add(Integer.parseInt(num)); //add the item to the list
}

答案 3 :(得分:1)

文本可能包含一些非数字字符,因此您应该检查它们,所以请尝试这样

      public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        List<Integer> nextList = new ArrayList<>();
        File file = new File("list.txt");
        BufferedReader reader = null;
        try {
          reader = new BufferedReader(new FileReader(file));
          String text;
          while ((text = reader.readLine()) != null) {// read the list of number from file
            if(!text.isEmpty()) {
              try {
                if (text.contains(",")) {
                  for (String str : text.split(",")) {
                    list.add(Integer.parseInt(str));
                  }
                }
                else if(text.matches("[+-]?[0-9]+")) {
                  list.add(Integer.parseInt(text));
                }
                else {
                  System.out.println("this is not a number : " + text);
                }
              } catch (NumberFormatException e){
                System.out.println("this is not a number : "+ text);
              }
            }
          }
          for (int i = 0; i < list.size(); i++) {// loop through each number
            int num = list.get(i);
            int max = Integer.MAX_VALUE;// get the next max value of each number
            for (int j = 0; j < list.size(); j++) {
              if (num < list.get(j) && list.get(j) < max) {
                max = list.get(j);
              }
            }
            nextList.add(max);
          }
          for (int i = 0; i < list.size(); i++) {// print the numbers
            System.out.println(list.get(i) + " : " + nextList.get(i));
          }
        }
        catch (FileNotFoundException e) {// if file not found
          e.printStackTrace();
        }
        catch (IOException e) {
          e.printStackTrace();
        }
        finally {// close the file at the end
          try {
            if (reader != null) {
              reader.close();
            }
          }
          catch (IOException e) {
          }
        }
      }