2014-03-12 97 views
0

因此,当标题说我想解析一个字符串值为浮点值,但我得到这样的数字格式异常无效浮点数:“3 “5”2“实际人数是3.52。哦,即时通讯在片段中完成所有这些都会给我带来麻烦。即使将它保存到文件,Im也不会在此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()); 
       } 

     } 
    }); 
+0

好,你是从一些文件中读取,检查文件是否包含'线3'。?? 5 ?? 2' – donfuxx

+0

编码问题? –

+0

文件本身显示没有问号或者它们不可显示。我只是使用input.getEncoding()检查了InputStreamReader,并且它每次显示我UTF8 –

回答

0

试试这个:

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

或使用'StandardCharsets.UTF_8' – donfuxx

+0

更改我的编码不会帮助我的InputStreamReader输入已经使用U TF8(使用input.getEncoding()检查) –