1

I encountered the following issue when trying read numbers from a csv file. The numbers are well formed and decimal points are also correct (dots):

10;111.1;0.94
9.5;111.1;0.94
9;111.4;0.94
8.5;110.7;0.94

I read the file line by line and split each of them into three tokens, free of white spaces etc. (e.g. "10","111.1","0.94"). In spite of this I got the exception when calling a parsing function:

             Double pwr = Double.parseDouble(tokens[1]);
             Double control = Double.parseDouble(tokens[0]);

             Double cos = Double.parseDouble(tokens[2]);

java.lang.NumberFormatException: For input string: "10"

When I change the order of lines, e.g., 1 <--> 2, the problem persists, but now I got java.lang.NumberFormatException: For input string: "9.5" What is interesting, every time I make the above calls from the debugger level, I obtain correct values with no exception. It looks like a problem related to the first line of file.

Have you any idea where the problem source is?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 3
    Seems like you've got some kind of invisible character at the very beginning of your file. – Dawood ibn Kareem Apr 13 '18 at 09:10
  • 1
    yes, you should post your numbers array as it's in the code – payloc91 Apr 13 '18 at 09:11
  • Iterate over the tokens' char-array. Maybe you will find a unexpected char. – Flocke Apr 13 '18 at 09:12
  • Probable encoding issue. There is an invisible character before the numbers. https://www.fileformat.info/info/unicode/char/200B/index.htm – Bejond Apr 13 '18 at 09:17
  • You probably have a `unicode` character at the beginning of the line. Just to verify you can print the line as it is and copy the output to a string variable in your code then you'll be able to see the which character it is. – Saif Ahmad Apr 13 '18 at 09:20
  • Step through this with a debugger, so you that you can see the actual characters that make up each `String` that you're trying to parse. – Dawood ibn Kareem Apr 13 '18 at 09:23

2 Answers2

0

It's probably a non-printable ASCII character

To remove this, you can simple use replaceAll method like this and use the following regex to remove \\P{Print}

BufferedReader br = new BufferedReader(new FileReader(""));
String str=br.readLine();
str=str.replaceAll("\\P{Print}", "");

After running the following RegEx you should be able to parse the value

===========================================================================

To see which character it is you can try this.

1) Read the line and print it as it is like this.

public class Test {
    public static void main(String[] args) {

        try {
            BufferedReader br = new BufferedReader(new FileReader("/path/to/some.csv"));
            String str=br.readLine();
            System.out.println(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

OUTPUT:

enter image description here

2) Now copy output as it is and paste it inside a ""(double quotes)

enter image description here

As you can see the special character is visible now

Saif Ahmad
  • 1,118
  • 1
  • 8
  • 24
0

[SOLVED] Presumably it was a problem of some "hidden" zero-length character at the beginning of the file (BTW, thank you for the helpful suggestions!). I changed the file encoding to UTF-8 (Menu > File > File Encoding) and that resolved the issue.