Reputation: 351
my program use a Write to save a double(ratingbar.rating) and a read to put the saved file inside a array. Then, with the array, I do an average of the numbers inside the array. In the read code, when I had getResources("raw.file") was working fine and the return was correct, but now I need to write the numbers and for that I can't use the res folder, so now I'm using the Internal Storage and my average returns me INFINITY. The average is called in a button OnClickListener. I'll post the code:
Write
private void writeMyArray(double rate){
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("arraymedia.txt", true)));
//.println(rate);
double ratex2 = rate * 2;
int erate = (int)ratex2;
try{
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("myarray.txt", Context.MODE_APPEND));
outputStreamWriter.append(Integer.toString(erate));
outputStreamWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Read
private void readMyArray(ArrayList<Double>array){
String ret = "";
try {
InputStream inputStream = openFileInput("myarray.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
int enc = 0;
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
array.add(enc, Double.parseDouble(receiveString));
++enc;
}
inputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Average
private double media(ArrayList<Double>array)
{
double total = 0;
double media;
int a = 1;
for (int i=0; i<array.size(); i++)
{
total = total + array.get(i);
a=i;
}
media = total / a;
return media;
}
Button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rating = ratingbar.getRating();
writeMyArray(rating);
readMyArray(arraydays);
button.setText(getText(R.string.obrigado) + "!" + media(arraydays));
//button.setText(getText(R.string.obrigado)+"!");
ratingbar.setEnabled(false);
button.setEnabled(false);
}
});
Upvotes: 2
Views: 433
Reputation: 26961
In your loop there is a mistake, dont use a=i
, that cause (if only one element) a=0
with result of infinity. use i+1
instead to add 1
in each iteration
int i = 0;
for (i=0; i<array.size(); i++)
{
total = total + array.get(i);
// a=i; // wrong
}
media = total / (i + 1);
return media;
Upvotes: 0
Reputation: 2696
private double media(ArrayList<Double>array){
double total = 0;
double media;
int a = array.size(); // set to size of array, instead of setting to i
for (int i=0; i<array.size(); i++) {
total = total + array.get(i);
}
if(a==0) //if a == 0 // divide by 0 -> infinity
media = 0;
else
media = total / a;
return media;
}
Upvotes: 0
Reputation: 2953
Division amounts to infinity in java if your denominator is 0.0 (0 in float or double). Please check before dividing.
Upvotes: 3