可以将字符串数组的所有值添加为正数或负数,然后将它们传递给双数组。
实施例: 和价值 String sa_notas [] = {1,5,-2} 结果(1 + 5-2)= 4
错误消息: 在org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:149) 在org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:281) 在java.lang.Double.parseDouble(Double.java:318) 在es.amedidaapp.gado.D_Anadir_Registro $ 3.afterTextChanged(D_Anadir_Registro.java:176)
感谢您的帮助
代码:
if (s.length() > 0) {
s_contains_notas = et_notas.getText().toString();
if (b_calculadora) {
if (s_contains_notas.length() <= 0 || !s_contains_notas.matches("[^0-9]+")) {
s_contains_notas = s_contains_notas.replace("+", " ");//separate positives numbers
s_contains_notas = s_contains_notas.replaceAll("[^0-9, -]", ""); //delete letters
s_contains_notas = s_contains_notas.replace(",", "."); //change format numbers
s_contains_notas = s_contains_notas.replace("-", " -"); //separate negatives numbers
String sa_notas[] = s_contains_notas.split(" "); //{1, 5, -2}
double[] d_notas = new double[sa_notas.length];
for (int i = 0; i < d_notas.length; i++) {
d_notas[i] = Double.parseDouble(sa_notas[i]); //ERROR to write negative numbers
}
double d_sum_notas = 0;
for (Double d : d_notas) {
d_sum_notas += d;
}
s_euros = String.valueOf(d_sum_notas);
et_euros.setText(nf_locale.format(d_sum_notas));
}
}
}
}
答案 0 :(得分:1)
一个不同的方法怎么样?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegEx {
// Example input:
final static private String NUMBERS = "Data: 0,5€ -0.1$ 9 -3 #3";
// Regular expression to match floating point numbers:
final static private Pattern PATTERN = Pattern.compile("[-]?[0-9]+(?:[,.][0-9]+)?");
public static void main(String[] args) {
final Matcher m = PATTERN.matcher(NUMBERS);
while ( m.find() ) {
System.out.println(m.group());
}
}
}
输出:
0,5
-0.1
9
-3
3
答案 1 :(得分:0)
感谢JimmyB指导我,
下一个问题,新代码无法生成小数。
新代码
if (s_contains_notas.length() <= 0 || !s_contains_notas.matches("[^0-9]+")) {
List<String> list = new ArrayList<>();
Pattern PATTERN = Pattern.compile("[-]?[0-9]+(?:[,.][0-9]+)?");
Matcher m = PATTERN.matcher(s_contains_notas);
while ( m.find() ) {
list.add(m.group());
}
double[] d_notas = new double[list.size()]; //create an array with the size of the list
for (int i = 0; i < list.size(); ++i) { //iterate over the elements of the list
d_notas[i] = Double.parseDouble(list.get(i)); //store each element as a double in the array
}
double d_sum_notas = 0;
for (Double d : d_notas) {
d_sum_notas += d;
}
s_euros = String.valueOf(d_sum_notas);
et_euros.setText(nf_locale.format(d_sum_notas));
答案 2 :(得分:0)
问题解决了
CODE:
if (s_contains_notas.length() <= 0 || !s_contains_notas.matches("[^0-9]+")) {
list = new ArrayList<>();
Pattern PATTERN = Pattern.compile("[-]?[0-9]+(?:[,.][0-9]+)?");
s_contains_notas = s_contains_notas.replaceAll("," , ".");
Matcher m = PATTERN.matcher(s_contains_notas);
while (m.find()) {
list.add(m.group());
}
double[] d_notas = new double[list.size()]; //create an array with the size of the failList
for (int i = 0; i < list.size(); ++i) { //iterate over the elements of the list
d_notas[i] = Double.parseDouble(list.get(i)); //store each element as a double in the array
}
double d_sum_notas = 0;
for (Double d : d_notas) {
d_sum_notas += d;
}
s_euros = String.valueOf(d_sum_notas);
et_euros.setText(nf_locale.format(d_sum_notas));
}
输出: 1 -1 0.5 -0.5 0,5