2017-08-18 2 views
-2

このブール値配列を保存しようとしています。私は配列を読んだとき、文字列の配列(部品)は、私がBoolean.parseBoolean配列を使用する場合Boolean.parseBoolean not working

 parts[0]=true; 

が、[0]まだ偽であると述べています。誰かが私を助けて、私が間違っていることを教えてもらえますか?お願いしてありがとう。

public void writeArraytofile() { 
    try { 
     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("array.txt", Context.MODE_PRIVATE)); 
     outputStreamWriter.write(Arrays.toString(array)); 
     outputStreamWriter.close(); 
    } catch (IOException e) { 
     Log.v("MyActivity", e.toString()); 
    } 
} 
public boolean[] read(){ 

String result = ""; 
boolean[] array = new boolean[2]; 
try { 

    InputStream inputStream = openFileInput("array.txt"); 

    if (inputStream != null) { 
     InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
     BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
     String tempString = ""; 
     StringBuilder stringBuilder = new StringBuilder(); 

     while ((tempString = bufferedReader.readLine()) != null) { 
      stringBuilder.append(tempString); 
     } 

     inputStream.close(); 

     result = stringBuilder.toString(); 

     String[] parts = result.split(" "); 
     for (int i = 0; i < array.length; i++){ 
      array[i]=Boolean.parseBoolean(parts[i]); 

     } 

    } 


} catch (FileNotFoundException e) { 
    Log.v("MyActivity", "File not found" + e.toString()); 
} catch (IOException e) { 
    e.printStackTrace(); 
} catch (NumberFormatException e) { 
    //here you catch and watch the problem 
    Log.e("MyActivity", "cant parse string: " + result); 
} 
return array; 
} 
+2

'parts [0] = true'はコンパイルされません – anomeric

+0

@anomericなぜですか? –

+1

それは文字列配列なので... – anomeric

答えて

0

Arrays.toString()は、ブラケットとカンマが印刷されます、あなたが後ろに文字列を読み込み、.split(" ")を呼び出すときに、最初の部分は"[true,"になります。それは"true"だけではないので、Boolean.parseBoolean()falseを返します。