2016-10-11 6 views
0

Как можно добавить все значения массива строк, являющиеся этими положительными или отрицательными, а затем передать их в двойной массив.Parse String to double []

Пример: SUM ЗНАЧЕНИЯ Строка sa_notas [] = {1, 5, -2} результат (1 + 5-2) = 4

ERROR MENSSAGE: на org.apache.harmony.luni .util.FloatingPointParser.initialParse (FloatingPointParser.java:149) at org.apache.harmony.luni.util.FloatingPointParser.parseDouble (FloatingPointParser.java:281) at java.lang.Double.parseDouble (Double.java:318) at 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

Благодарим Вас за ответ. Если я удаляю разделители между этими номерами, которые они соединяли. (152) – d2000k

+0

В чем проблема? Какой результат вы получаете? – JimmyB

+0

Когда я пишу отрицательные числа, потратив его на удвоение, он дает ошибку – d2000k

ответ

0

проблема решена

КОД:

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 0,5 -0,5 0,5

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 
+0

как значения суммы? – d2000k

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));