简体   繁体   中英

Java NumberFormatException in list reading application

I wrote code for a program that reads a file containing a list of numbers and outputs, for each number in the list, the next bigger number. I'm using eclipse for this project and when i go an run the program I'm getting an error and i cant seem how to fix it.

The error I am getting is:

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)

Here's my code:

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

You need to split ( and also trim() ) your line over a comma (,) and then parse over all the elements you obtain. Hope this solves your query.

The issue is you are trying to convert a string with multiple numbers to a single integer, which is causing your exception. Try this instead:

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

The text could have some non numeric characters so you should check about them so try like this

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

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