我有一个.txt包含

bananas, 1, 15
dogs, 1, 10
cats, 1, 5


使用split(", ")方法,我能够在数组15的第一行中获取第一个String[] price,但是我也想存储最后两个数字。我在想一个二维数组

price[0][2] = 15,
price[1][2]=10
price[2][2] = 5


然后将这三个解析为双精度并将它们加在一起。我有这个,


        while ((linePrice = totalReader.readLine()) != null) {
            price1 = linePrice.split(", ");

            if ((line = totalReader.readLine()) != null) {
                price2 = linePrice.split(", ");
            }

            if ((line = totalReader.readLine()) != null) {
                price2 = linePrice.split(", ");
            }


        }


但是它什么也做不了,因为这三个价格都是第一个,15

最佳答案

您只需要简单地遍历文本文件中的每一行并通过在,上分割行并附加到价格数组来获取价格,下面的伪代码可以帮助您开始

String[] prices = new String[3];
int i = 0;
while ((linePrice = totalReader.readLine()) != null) {
    String[] array = linePrice.split(", ");
    prices[i] = array[2]; // third index contain price
    i++;
}

07-24 20:53