从String解析为Float会返回带有问号的浮点值

时间:2014-03-12 19:39:57

标签: android

因为标题说我试图将字符串值解析为浮点值,但我得到一个数字格式异常,如 无效浮点数:" 3 ??。?? 5 ?? 2" 实际数字是3.52。哦,我正在片段中做这一切,这让我烦恼。我将它保存到文件时,我也没有在此Float值上使用任何DecimalFormats。那我做错了什么? :(
我正在读/写文件的方式如下:

file = new File(Environment
            .getExternalStorageDirectory()
            + File.separator
            + "userZinsenArray.txt");

 save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String outputString = "";
            try {

                if (!file.exists())
                    file.createNewFile();
                System.out.println("file exists:" + file.exists());
                FileOutputStream stream = new FileOutputStream(file);
                DataOutputStream ps = new DataOutputStream(stream);
                for (int i = 0; i < MainActivity.prozentenArray.size(); i++) {
                    outputString+=(""+MainActivity.prozentenArray.get(i));
                    outputString+="\n";
                }
                ps.writeChars(outputString);
                ps.close();
                stream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

load.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

             String line=null;
                try {
                    InputStream in = new FileInputStream(file);
                    InputStreamReader input = new InputStreamReader(in);
                    BufferedReader buffreader = new BufferedReader(input);
                    while (( line = buffreader.readLine()) != null) {
                        MainActivity.prozentenArray.add(Float.parseFloat(line));                            
                    }
                    in.close();
                } catch(NumberFormatException e){
                   System.out.println("numberFormatException: "+e.getMessage());
                }catch(Exception e){
                    System.out.println("other exp: "+e.getMessage());
                }

        }
    });

1 个答案:

答案 0 :(得分:0)

试试这个:

InputStreamReader input = new InputStreamReader(in, "UTF-8");