Update...Delimiter is showing up in my array values caused by Platform or Emulator
When I debug the code below it stores the "," (split delimiter) as a value in the array rawData
. When parseDouble
gets to that value I get a NumberFormatException
. Any ideas?
package com.simplydesign.android.standarddev;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button addBtn = (Button) findViewById(R.id.addBtn);
final EditText userData = (EditText) findViewById(R.id.editText1);
final TextView dataCollection = (TextView) findViewById(R.id.textView1);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] rawData;
rawData = userData.getText().toString().split(",");
dataCollection.setText(calculate(rawData));
}
});
}
public String calculate(String[] a) {
double[] convertedData = new double[a.length];
double sum = 0.0;
StringBuilder output = new StringBuilder();
int t = 0;
for (int f = 0; f<a.length;f++){
convertedData[f] = Double.parseDoub开发者_如何学运维le(a[t]);
t++;
}
for (int k = 0; k < convertedData.length; k++) {
sum += convertedData[k];
}
output.append(sum);
return output.toString();
}
}
After a lot of trial and error I have concluded that my problem was coming from a faulty platform / emulator combination. Originally I had my project platform set to 2.1 with an API value of 7.
For whatever reason this was causing problems when I used a comma as a delimiter. It also gave me problems when my numbers had decimal points (the decimal points looked hollow).
Now I have my project set to 1.6 and it works with the comma and decimal points.
Although I found the solution I still don't understand why this is happening. I feel like some of the apps I create need to be on a high platform, at least 2.5. Is 1.6 ok for most apps? Any suggestions on this matter would be GREATLY appreciated.
Try to use TextUtils.split
精彩评论