2016-10-11 81 views
0

可以將字符串數組的所有值加上正數或負數,然後將它們傳遞給雙數組。Parse String to double []

實施例: 和值 字符串sa_notas [] = {1,5,-2} RESULT(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

問題解決

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

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