我的程序中有这个问题: 当我这样做时:
double a = Double.parseDouble(zero);
double b = Double.parseDouble(data);
double c = a+b;
String d = Double.toString(c);
它返回String
。例如,我在a
中有0.0,在b
中有4.0,它返回0.04.0
。
有人可以向我解释为什么以及我应该做些什么呢?
编辑:这是我的代码:
private void WriteMedia(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("writemedia.txt", Context.MODE_PRIVATE));
String zero;
if(ReadMedia().isEmpty() == true)
{
zero = "0.0";
outputStreamWriter.write(zero);
}
else {zero = ReadMedia();}
double a = Double.parseDouble(zero);
double b = Double.parseDouble(data);
double c = a+b;
String d = Double.toString(c);
outputStreamWriter.write(d);
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
private String ReadMedia() {
String ret = "";
try {
InputStream inputStream = openFileInput("writemedia.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("estrela.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
WriteMedia(data);
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput("estrela.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
WriteMedia(ret);
return ret;
}
数据是评级栏,零是" 0"当我启动应用程序时,它应该将评级栏保存到"增加"(不知道英文单词)。我觉得这很容易但是因为就像我的错误一样,我会把标签安装在android工作室。
以下是我称之为方法的地方:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rating = ratingbar.getRating();
writeToFile(Double.toString(rating));
writeMyArray(Double.parseDouble(readFromFile()));
writeMyArray(ratingbar.getRating());
button.setText(getText(R.string.obrigado) + "!" + readFromFile() + " " + media(arraydays) + " " + ReadMedia());
}
});
它返回" 0.0"和评级栏在一起,例如:" 0.05.0"
答案 0 :(得分:5)
您的问题不与类型相关(您猜不到),并且您最初发布的代码中的不是。
相反,您只需将 2 数字写入输出流。以下代码写入0.0
:
if(ReadMedia().isEmpty() == true)
{
zero = "0.0";
outputStreamWriter.write(zero);
}
并且,在添加中,以下代码会写出实际想要的结果:
double a = Double.parseDouble(zero);
double b = Double.parseDouble(data);
double c = a+b;
String d = Double.toString(c);
outputStreamWriter.write(d);