Am trying to get these two peaks once I read from my text file. I have defined two string to compare two value in order to find the peak, but I think my if(y_0>MP) statement is not correct one to find the two peak. What should I do please.
//Read data.
Recall = (Button) findViewById(R.id.Recall);
Recall.setOnClickListener(new View.OnClickListener() {
StringBuilder stringBuilder;
@Override
public void onClick(View v) {
//readTextFile(this, R.raw.books);
stringBuilder = new StringBuilder();
String line;
try {
FileInputStream fIn = new FileInputStream(file);
BufferedReader bufferedreader = new BufferedReader(new
InputStreamReader(fIn));
while ((line = bufferedreader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(" ");
stringBuilder.length();
}
bufferedreader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String a;
a = String.valueOf(stringBuilder);
String dataArray[];
int t;
dataArray = a.split(" ");
int MP = 0;
for (t = 1; t < dataArray.length - 1; t++) {
float y_0 = Float.valueOf(dataArray[t]);
float y_1 = Float.valueOf(dataArray[t + 1]);
float y_2 = Float.valueOf(dataArray[t - 1]);
float left = y_0 - y_2;
float right = y_1 - y_0;
if (left > 0 && right < 0) {
if (y_0 > MP) {
MP = (int) y_0;
} else {
MP = (int) y_0;
}
Toast.makeText(getApplicationContext(), "Number of peaks
founds\n: " + MP, Toast.LENGTH_SHORT).show();
}
}
DataAlert alert = new DataAlert();
alert.show(getFragmentManager(), "DataAlert");
}
});
Your suspicions about the if (y_0 > MP) line are correct. If you want to make a toast showing the number of peaks found, then you either need to keep a list of the peaks, or a counter, and add to it every time a peak is found. Then, after the for-loop has finished searching for peaks, you would make a toast saying how many peaks were found.
For future reference, this section of code
Is equivalent to this:
You assign
MP = (int) y_0
regardless of whether the if statement is true or false.