Error java.lang.NumberFormatException: For input string: ""
Solution 1
You're splitting on a single space but there is more than one space between the integer columns
espacios = reader.readLine().trim().split("\\s+");
Solution 2
You got to make sure that espacios[0]
through espacios[3]
are actually integers. What you should probably do is print them out to check. If one of them isn't an integer, Integer.parseInt()
will throw that exception.
ArCiGo
Currently working as a Test Engineer at Luxoft. Looking to grow as an Automation Test Engineer, work with amazing technologies, and work in the European Union (Germany, Sweden, or Spain). Science Popularizer at Sólo es Ciencia. Math/Testing/Career Switching mentor. I have worked as a: Software Engineer in Test (Manual Tester) at iTexico. Software Engineer Intern at Unosquare. Software Engineer Intern at Nectri, using .NET technologies. Tester (Researcher Assistant) at ITESM Monterrey (Campus Monterrey). Speaker.- Panel: Why is testing important? [Hands on Testing (QA Minds), 2021] Retos del Ingeniero en Sistemas Computacionales [Instituto Tecnológico de Cd. Madero, 2021] How to create an amazing CV! [Sólo es Ciencia Fest, 2021] Dabbling in the Marvelous World of IT! [PosaDev, 2020] Test your f*** code! (Why do you need to test your code?). [Luxoft Tech Talks (Luxoft), 2020] Unit What!? (Unit Testing with C#). [Digital Economy Summit, 2019; PosaDev, 2019] That’s not a bug, it's a feature… (Why is testing necessary?). [Headspring Talks (Headspring), 2019] On the other hand, I've participated in activities like helping/visiting hospitals, foundations, and nursing homes; I used to be a Catholic missionary in rural communities. «I like to help other people to learn and grow their skills in the IT field 😊» - ArCiGo «Sometimes I find bugs where there shouldn’t be.» - ArCiGo «The people have a lot of bugs, including me. But there is nothing to worry about, we are perfectible 😊» - ArCiGo
Updated on November 08, 2020Comments
-
ArCiGo over 3 years
I'm trying to read this file
C101 VEHICLE NUMBER CAPACITY 25 200 CUSTOMER CUST NO. XCOORD. YCOORD. DEMAND READY TIME DUE DATE SERVICE TIME 0 40 50 0 0 1236 0 1 45 68 10 912 967 90 2 45 70 30 825 870 90 3 42 66 10 65 146 90 4 42 68 10 727 782 90 5 42 65 10 15 67 90 6 40 69 20 621 702 90 7 40 66 20 170 225 90 8 38 68 20 255 324 90
But when I read the file, the program throws me this exception:
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:504) at java.lang.Integer.parseInt(Integer.java:527) at VRP.main(VRP.java:43) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)
I cheked my code and debugged, but I can't find the problem.
I used an array to save all the spaces between the columns (I only read the first four columns) and then an ArrayList but it doesn't worked.
public static void main(String[] args) throws Exception { File f = new File("C101.txt"); FileReader fr = new FileReader(f); BufferedReader reader = new BufferedReader(fr); String nada = reader.readLine(); //C101 nada = reader.readLine(); //espacio en blanco nada = reader.readLine(); //vehicle=25 nada = reader.readLine(); //number, capacity nada = reader.readLine(); //25, 200 nada = reader.readLine(); //espacio en blanco nada = reader.readLine(); //customer nada = reader.readLine(); //encabezados nada = reader.readLine(); //espacio en blanco String[] espacios; int capacity = 200; int custno, xcoord, ycoord, demand; ArrayList<String> guardar = new ArrayList<String>(); while (reader.ready()) { espacios = reader.readLine().trim().split(" "); for (int i = 0; i < espacios.length; i++) { guardar.add(espacios[i]); } custno = Integer.parseInt(espacios[0]); xcoord = Integer.parseInt(espacios[1]); ycoord = Integer.parseInt(espacios[2]); demand = Integer.parseInt(espacios[3]); } }
Sorry for the inconvenient and thank you for your time.
-
ArCiGo over 10 yearsBut all the numbers are Integers
-
Hovercraft Full Of Eels over 10 years@ArCiGo: you've got to either use a debgger or println anything. Throw all assumptions out the window. 1+ to the answer.
-
ArCiGo over 10 yearsI found the mistake: at the end of the .txt file a blank space was left by the author
-
Reimeus over 10 yearsIt's likely that that this fixed the problem but that the extraneous line at the end of the file caused the same problem later while reading the file