[java] Got a NumberFormatException while trying to parse a text file for objects

While trying to create objects and add them to an ArrayList using data from a text file, the program threw a NumberFormatException and I have absolutely no clue why, everything seemed OK to me. Here's the method in which the exception occurred:

static void read(String file) {     anime.clear();      try {         Scanner fin = new Scanner(file);          while (fin.hasNextLine()) {             String[] vals = fin.nextLine().split("[ ]");             anime.add(new Anime(Integer.parseInt(vals[0]),                                  vals[1],                                  Integer.parseInt(vals[2]),                                  Integer.parseInt(vals[3])));         }         fin.close();      } catch(Exception e) {         System.out.println("ERROR: Something went wrong!");         e.printStackTrace();         System.exit(-1);     }        } 

and here's the text file:

0 Angel_Beats! 13 2010 0 Baccano! 13 2007 0 Bakemonogatari 15 2009 0 Berserk 25 1997 0 Clannad 23 2007 

This question is related to java parsing numberformatexception

The answer is


I changed Scanner fin = new Scanner(file); to Scanner fin = new Scanner(new File(file)); and it works perfectly now. I didn't think the difference mattered but there you go.


The problem might be your split() call. Try just split(" ") without the square brackets.


NumberFormatException invoke when you ll try to convert inavlid String for eg:"abc" value to integer..

this is valid string is eg"123". in your case split by space..

split(" "); will split line by " " by space..


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to parsing

Got a NumberFormatException while trying to parse a text file for objects Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) Python/Json:Expecting property name enclosed in double quotes Correctly Parsing JSON in Swift 3 How to get response as String using retrofit without using GSON or any other library in android UIButton action in table view cell "Expected BEGIN_OBJECT but was STRING at line 1 column 1" How to convert an XML file to nice pandas dataframe? How to extract multiple JSON objects from one file? How to sum digits of an integer in java?

Examples related to numberformatexception

Got a NumberFormatException while trying to parse a text file for objects How can I prevent java.lang.NumberFormatException: For input string: "N/A"? Convert hex color value ( #ffffff ) to integer value How to avoid Number Format Exception in java? Double value to round up in Java