Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

The code for buffered reader that reads numbers from file and sorts them is:

package inout;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BufferedReaderFrmFile 
{

	public static void main(String[] args) throws IOException {
			
                        
                        BufferedReader br = new BufferedReader(new FileReader("F:\\nu.txt"));
                        List<Integer> numbers = new ArrayList<Integer>();
                        String line = null;                        

                        while ((line = br.readLine()) != null) {
                        String []strNumbers = line.split(" ");
                        for(String strNumber : strNumbers){
                        numbers.add(Integer.parseInt(strNumber));
                    }         
     }   
     br.close();

     Collections.sort(numbers);
     System.out.println(numbers);
        }
}


the program gives the error
java.lang.NumberFormatEsception: For input string: ""


nu.txt has numbers
12 12 12 857 85 3 8 6 8 56 23 45 89 23 23 02 15
45 41 56 56 89 56 23 45 56 23 45 78 56 45 23

What I have tried:

changed code and the type of source file
Posted
Comments
Maciej Los 3-May-18 13:43pm    
Have you trid to debug your programme?
Richard MacCutchan 3-May-18 13:52pm    
You cannot convert an empty string into an integer. You should check the string before you try to parse it, to see if it is a valid number. Alternatively catch the exception and handle it.
four systems 4-May-18 11:57am    
would have to do it another way
thanks
Maciej Los 4-May-18 15:22pm    
I have to repeat: Have you trid to debug your programme?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900