简体   繁体   中英

java.lang.NumberFormatException: For input string: “4�⌷ 3⌷�9�⌷.�8⌷�9⌷” at sun.misc.FloatingDecimal.readJavaFormatString

I'm having problems reading from a csv file which contains numbers with the format #.##,#.## those are randomly generated doubles (coordinates) wrote into the file as Strings by another program.

here's the part of the code that I use to read from the file and parse the coordinates:

    private void populateData() {

    for (int i = 0; i < this.data[0].length; i++) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("Sample.csv"));
            while (br.readLine() != null) {
                String[] split = br.readLine().split(",");
                this.data[0][i] = Float.parseFloat(split[0].trim());
                this.data[1][i] =Float.parseFloat(split[1].trim());
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FastScatterPlotDemo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FastScatterPlotDemo.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

}

it gives me a weird exception

Exception in thread "main" java.lang.NumberFormatException: For input string: "4⌷�3⌷�9⌷�.⌷�8⌷�9⌷"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Float.parseFloat(Float.java:422)
at lab05b_Task2.FastScatterPlotDemo.populateData(FastScatterPlotDemo.java:86)
at lab05b_Task2.FastScatterPlotDemo.<init>(FastScatterPlotDemo.java:41)
at lab05b_Task2.FastScatterPlotDemo.main(FastScatterPlotDemo.java:107)

I don't know if it has something to do with the fact that I'm working on ubuntu and it's an encoding problem.

You appear to have a UTF-16 encoded file. You have to determine if it is UTF-16LE or UTF-16BE. I would guess UTF-16LE first. I suggest you set the encoding to match the file and you should read it correctly. If you don't know what the encoding should be, ask the source of the file what it/they used.

BTW don't use float if you can avoid it. Try double or BigDecimal instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM