-4

I've been getting a NumberFormatException error when I try to read some data from a text file and convert it to an integer. From what I've seen other people say, this error is caused when an empty string is converted to an integer using pasreInt(). But I've been able to print the string '1' from the file into the output. Does anyone know why I'm getting this error even though the string doesn't seem to be empty? Here's my code:

try {
        //Retrieve Info
        FileReader fr = new FileReader("BankInfo.txt");
        BufferedReader br = new BufferedReader(fr);
        //Skip specified number of lines
        for(int i=0; i<line; i++) {
            br.readLine();
        }

        //Print the string to output
        String holderStr = br.readLine();
        System.out.println(holderStr);

        //The line creating the NumberFormatException
        totalBalNum = (double)Integer.parseInt(holderStr);

        br.close();
        //Read Whole File
        BufferedReader br2 = new BufferedReader(fr);
        while((str = br.readLine()) != null) {
            arrList.add(str);
        }
        br2.close();
    } catch (IOException | NumberFormatException e) {
        System.out.println("ERROR! Problem with FileReader. " + e);
    }

I know my code is probably really sloppy and inefficient too... I'm a little bit of a noob.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • try `holderStr.trim()` before passing and why are you casting again to Double ? There may be some extra characters also in your string so those can be reason for `NumberFormatException` – Deepak Aug 12 '16 at 03:31
  • Just to be clear the variable "totalBalNum" is integer or a double? – Vipul Behl Aug 12 '16 at 03:31
  • Possible duplicate of [How to resolve java.lang.NumberFormatException: For input string: "N/A"?](http://stackoverflow.com/questions/18711896/how-to-resolve-java-lang-numberformatexception-for-input-string-n-a) – soorapadman Aug 12 '16 at 03:33
  • Can you provide Stacktrace of the Exception? – ravthiru Aug 12 '16 at 03:35
  • @Deepak totalBalNum is a double. I've already experimented with that and casting it to a double isn't causing the error. Also trimming the string didn't work. – Tech Bandit Aug 12 '16 at 03:36
  • @VipulBehl totalBalNum is a double. – Tech Bandit Aug 12 '16 at 03:42
  • @TechBandit no that should not cause problem . Alternatively you can use `Double.parseDouble` also. You have to debug for the content what you are trying to convert as Integer . NumberFromatException comes when you try to convert anything to Number and that is not a number – Deepak Aug 12 '16 at 03:43
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Oct 14 '16 at 10:12

2 Answers2

0

Ok, I think converting string into Integer and then typecasting it to double is causing the error. Why don't you just convert the string to double. Also you must trim the line while reading it to avoid any spaces.

    String holderStr = br.readLine().trim();
    System.out.println(holderStr);

    totalBalNum = Double.parseDouble(holderStr);
Vipul Behl
  • 644
  • 1
  • 7
  • 20
0

use replaceAll() to convert everything but digits to empty character.

holderStr.replaceAll("\\D+","");

eg.

String extra34345 dfdf will be converted to 34345
String ab34345ba will be converted to 34345
String \n34345\n will be converted to 34345

The code

String holderStr = br.readLine();

//this line will remove everything from the String, other than Digits
holderStr= holderStr.replaceAll("\\D+","");

System.out.println(holderStr);
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71