如何将文本文件中的String元素添加到ArrayList中?

时间:2013-10-12 22:02:42

标签: java arraylist text-files arrays

文本文件包含以逗号分隔的数字(例如458.58,1598.45 ......)。

我想将文本文件中的整行添加到ArrayList中。

到目前为止,这是我的代码:

// to calculate final output
ArrayList<String> weeklySales = new ArrayList<String>(7);

// week 1 sales
while(file.hasNext()) {
    weeklySales.add(file.nextLine());
    System.out.println("I ran!");
}

System.out.println(weeklySales);
编辑:抱歉我的问题不明确。我的问题是在运行此代码之后,它将ENTIRE文本文件的所有元素添加到我的数组中,但我只需要将1个LINE添加到它自己的单个ArrayList中。总的来说,我将拥有与文件中的文本行一样多的数组列表。

2 个答案:

答案 0 :(得分:0)

这是代码,您可以使用:

String line = "458.58, 1598.45";
String array[] = line.split(", ");

通过这种方式可以从String更改为double:

Double.valueOf(array[i]);

可以使用以下命令将数组更改为列表:

ArrayList<String> list = new ArrayList<>();
list.addAll(Arrays.asList(array));

答案 1 :(得分:0)

我终于开始工作了。我意识到使用StringTokenizer时,处理单个行并将单个数字与文本文件分开的最方便的方法。这是完全正常工作的代码,您需要的只是文本文件,名为'SalesData.txt',我的包含以下3行:

1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36 2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36

该程序输出以下内容:
- 每周总销售额 - 每周的平均日数
- 所有周的总销售额 - 平均每周总数
- 销售额最高的一周
- 销售额最低的一周

DecimalFormat formatter = new DecimalFormat("#0.00");

// create file object
File salesData = new File("SalesData.txt");

// open file
Scanner file  = new Scanner(salesData);

// declare 2 dimensional array 
double[][] weeklySales = new double[3][7];

// loop to initialize token
int row = 0;
while(file.hasNext()) {
// initialize token
String line = file.nextLine();
StringTokenizer tokens = new StringTokenizer(line, ",");

// fill columns and rows
int col = 0;
while(tokens.hasMoreTokens()) {
        // convert to double and assign to token
    weeklySales[row][col] = Double.parseDouble(tokens.nextToken());
    // move up 1 column
    col++;
    }
// move down 1 row
row++;
}

// calculate weekly sales
double week1Sales = 0, week2Sales = 0, week3Sales = 0;
for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 5; j++) {
        double daily = weeklySales[i][j];
        if(i == 0)
            week1Sales += daily;
        else if(i == 1)
            week2Sales += daily;
        else if(i == 2)
            week3Sales += daily;
    }
}
// week 1 sales
System.out.println("Week 1 total sales: $" +formatter.format(week1Sales));
// week 2 sales
System.out.println("Week 1 total sales: $" +formatter.format(week2Sales));
// week 3 sales
System.out.println("Week 1 total sales: $" +formatter.format(week3Sales));

// average daily for week 1
System.out.println("Daily average for week 1: $" +formatter.format(week1Sales / 7));

// average daily for week 2
System.out.println("Daily average for week 2: $" +formatter.format(week2Sales / 7));

// average daily for week 3
System.out.println("Daily average for week 3: $" +formatter.format(week3Sales / 7));

// total for all weeks
double weeklyTotal = week1Sales + week2Sales + week3Sales;

System.out.println("Total sales of all the weeks: $" +formatter.format(weeklyTotal));

// average weekly sales
System.out.println("Average weekly total: $" + formatter.format(weeklyTotal / 3));

// week number with highest sales
if(week1Sales > week2Sales)
    if(week1Sales > week3Sales)
        System.out.println("The week with the highest sales is week 1.");
    else
        System.out.println("The week with the highest sales is week 3.");
else if(week2Sales > week3Sales)
    if(week2Sales > week1Sales)
        System.out.println("The week with the highest sales is week 2.");
    else
        System.out.println("The week with the highest sales is week 1.");
else
    System.out.println("The week with the highest sales is week 3.");

// week number with the lowest
if(!(week1Sales > week2Sales))
    if(!(week1Sales > week3Sales))
        System.out.println("The week with the lowest sales is week 1.");
    else
        System.out.println("The week with the lowest sales is week 3.");
else if(!(week2Sales > week3Sales))
    if(!(week2Sales > week1Sales))
        System.out.println("The week with the lowest sales is week 2.");
    else
        System.out.println("The week with the lowest sales is week 1.");
else
    System.out.println("The week with the lowest sales is week 3.");