简体   繁体   English

如何使用for循环将字符串arraylist转换为double?

[英]How do you convert string arraylist to double using for loop to calculate?

I'm given the data.txt file and have to calculate the total amount using ArrayList 我得到了data.txt文件,必须使用ArrayList计算总数

my data.txt file contains: 我的data.txt文件包含:

32.14,235.1,341.4,134.41,335.3,132.1,34.1 32.14,235.1,341.4,134.41,335.3,132.1,34.1

so far I have 到目前为止,我有

public void processFile() throws IOException
{
    File file = new File("SalesData.txt");

    Scanner input = new Scanner(file);
    ArrayList<String> arr = new ArrayList<String>();
    String line = input.nextLine();

    StringTokenizer st = new StringTokenizer(line, ",");

    while (st.hasMoreTokens())
    {
        arr.add(st.nextToken());
    }

    setArrayListElement(arr); //calls setArrayListElement method

}

and here is my setArrayListElement method: 这是我的setArrayListElement方法:

private void setArrayListElement(ArrayList inArray)
{                
    for (int i = 0 ; i < inArray.size() ; i++)
    {
         // need to convert each string into double and sum them up
    }
}

Can I get some help?? 我可以帮忙吗?

  1. Never use doubles for monetary calculations (Previous answer is also wrong) 切勿将双精度数用于货币计算(先前的答案也是错误的)
  2. Never refer to a concrete class. 永远不要参考具体的课程。 The interface in this case is List arr = new ArrayList(); 在这种情况下,接口为List arr = new ArrayList();。

To your specific answer: 针对您的具体答案:

BigDecimal summed = BigDecimal.ZERO;

for (int i = 0 ; i < arr.size() ; i++) {
 final String value  = arr.get(i);
 try{
  BigDecimal bd = new BigDecimal(value);
  summed = summed.add(bd);
 } catch(NumberFormatException nfe){
       //TODO: Handle
 }
}

... ...

您必须使用Double.valueOf()

Double.valueOf(string);
Double value = Double.parseDouble(yourString);

I post you the method to calculate the total amount, dont forget to control exceptions. 我向您发布了用于计算总量的方法,请不要忘记控制异常。

    private Double setArrayListElement(ArrayList inArray) throws NumberFormatException
{   
    Double amount=0;    
    for (int i = 0 ; i < inArray.size() ; i++)
    {
       amount= amount+Double.valueOf(inArray.get(i));
    }
    return amount;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串ArrayList转换为Double ArrayList并计算值的总和? - How to Convert a String ArrayList to a Double ArrayList and calculate the sum of values? JAVA:如何将String变量转换为Double变量? - JAVA: How do you convert a String variable to a Double variable? 如何将整数的ArrayList转换为double []? - How do I convert an ArrayList of integers to a double[]? 如何在Java中使用for循环和if语句将字符串中的每个字母加倍 - How do you double each letter in a string using a for loop and an if statement in java 您如何将字符串转换为数组/数组列表 - How would you convert A string into A Array/ArrayList 如何将ArrayList转换为数组数组? - How do you convert an ArrayList to an array of arrays? 如何在 While 循环中递增 Integer 并将其转换为字符串? - How do you Increment an Integer inside a While loop and convert it to String? 如何在GWT中使用特定语言环境将字符串转换为Double? - How do I convert a String to Double in GWT using a specific locale? 如何使用特定语言环境在Java中将字符串转换为Double? - How do I convert a String to Double in Java using a specific locale? 如何转换 ArrayList<string[]> 到包含十进制值的双[]?</string[]> - How can I convert an ArrayList<String[]> to a double[] that contains decimal values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM